September 04, 2006

WPF - Style

Styles

  • lists of properties and values that enable consistency across an application
  • a style can be applied to any element in a user interface
  • a style can be used to set any property
  • styles can be applied only to framework elements

Example:



<Page x:Class="TestXBAP.Page1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Page1" Height="170.4" Width="354.4">

<StackPanel Margin="15" >

<StackPanel.Resources>

<Style x:Key="MyStyle" TargetType="{x:Type TextBox}">

<Setter Property="Background" Value="Orange" />

<Setter Property="Foreground" Value="White" />

</Style>

</StackPanel.Resources>

<TextBox Style="{StaticResource MyStyle}">TextBox 1 with Mystyle</TextBox>

<TextBox Style="{StaticResource MyStyle}">TextBox 2 with Mystyle</TextBox>

<TextBox>Unstyled Textbox</TextBox>

</StackPanel>

</Page>