wpf提供了一种类似css的对象- style,但是比css更强大。它支持直接设定属性,更改呈现模板,触发器,事件触发等。MSDN描述如下:
下面代码演示了 将button控件显示成圆形,设置背景色等。
代码
1 <Window x:Class="wpfAppStyleDemo.Window1"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Window1" Height="371" Width="550">
5 <Window.Resources>
6 <Style TargetType="Button">
7 <!--Set to true to not get any properties from the themes.-->
8 <Setter Property="OverridesDefaultStyle" Value="True"/>
9 <Setter Property="Background" Value="LightBlue" />
10 <Setter Property="Template">
11 <Setter.Value>
12 <ControlTemplate TargetType="Button">
13 <Grid>
14 <Ellipse Fill="{TemplateBinding Background}"/>
15 <ContentPresenter HorizontalAlignment="Center"
16 VerticalAlignment="Center"/>
17 </Grid>
18 </ControlTemplate>
19 </Setter.Value>
20 </Setter>
21 </Style>
22
23 <Style TargetType="Button" x:Key="btnStyle1">
24 <Setter Property="Background" Value="#66FF33"/>
25 </Style>
26
27 <Style TargetType="{x:Type TextBlock}">
28 <Setter Property="FontFamily" Value="Segoe Black" />
29 <Setter Property="HorizontalAlignment" Value="Center" />
30 <Setter Property="FontSize" Value="9pt" />
31 <Setter Property="Foreground" Value="#333333" />
32 <Setter Property="Background" Value="Yellow" />
33 </Style>
34
35 </Window.Resources>
36 <Grid>
37 <Grid.ColumnDefinitions>
38 <ColumnDefinition Width="277*" />
39 <ColumnDefinition Width="251*" />
40 </Grid.ColumnDefinitions>
41 <Button Height="23" HorizontalAlignment="Left" Margin="44,79,0,0" Name="button1" VerticalAlignment="Top" Width="75">Button</Button>
42 <Button Style="{StaticResource btnStyle1}" Height="23" HorizontalAlignment="Right" Margin="0,79,47,0" Name="button2" VerticalAlignment="Top" Width="75">Button</Button>
43 <TextBlock Margin="75,140,93,156" Name="textBlock1">
44 WPF演示,Style样式
45 </TextBlock>
46 </Grid>
47 </Window>
48
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Window1" Height="371" Width="550">
5 <Window.Resources>
6 <Style TargetType="Button">
7 <!--Set to true to not get any properties from the themes.-->
8 <Setter Property="OverridesDefaultStyle" Value="True"/>
9 <Setter Property="Background" Value="LightBlue" />
10 <Setter Property="Template">
11 <Setter.Value>
12 <ControlTemplate TargetType="Button">
13 <Grid>
14 <Ellipse Fill="{TemplateBinding Background}"/>
15 <ContentPresenter HorizontalAlignment="Center"
16 VerticalAlignment="Center"/>
17 </Grid>
18 </ControlTemplate>
19 </Setter.Value>
20 </Setter>
21 </Style>
22
23 <Style TargetType="Button" x:Key="btnStyle1">
24 <Setter Property="Background" Value="#66FF33"/>
25 </Style>
26
27 <Style TargetType="{x:Type TextBlock}">
28 <Setter Property="FontFamily" Value="Segoe Black" />
29 <Setter Property="HorizontalAlignment" Value="Center" />
30 <Setter Property="FontSize" Value="9pt" />
31 <Setter Property="Foreground" Value="#333333" />
32 <Setter Property="Background" Value="Yellow" />
33 </Style>
34
35 </Window.Resources>
36 <Grid>
37 <Grid.ColumnDefinitions>
38 <ColumnDefinition Width="277*" />
39 <ColumnDefinition Width="251*" />
40 </Grid.ColumnDefinitions>
41 <Button Height="23" HorizontalAlignment="Left" Margin="44,79,0,0" Name="button1" VerticalAlignment="Top" Width="75">Button</Button>
42 <Button Style="{StaticResource btnStyle1}" Height="23" HorizontalAlignment="Right" Margin="0,79,47,0" Name="button2" VerticalAlignment="Top" Width="75">Button</Button>
43 <TextBlock Margin="75,140,93,156" Name="textBlock1">
44 WPF演示,Style样式
45 </TextBlock>
46 </Grid>
47 </Window>
48
注意 sytle 的作用域 TargetType指定了style要适用到的“类型”,我们可以在这里指向控件。如果一个style没有 x:key 标记,那么它将适用在它的作用域下的所有控件类型。演示代码第23行和42行演示了 显式指定一个style的用法,值得一提的是,“如果显式的为一个控件指定了style ,那么 这个style具有更高的优先权”。本例中第42行的button仅仅作用了一个style,全局的(第6行声明的)style对它无效。
style的强大之处在这里体现,他可以更改一个控件的ControlTemplate,本例中 将一个矩形的button变成了椭圆。
style的trigger在上一篇文章里已经用到,本例就不再演示。
本节完
待续ing...