WPF入门(六)样式Style

wpf提供了一种类似css的对象- style,但是比css更强大。它支持直接设定属性,更改呈现模板,触发器,事件触发等。MSDN描述如下:

MSDN

可以在从 FrameworkElementFrameworkContentElement 派生的任何元素上设置 Style。样式通常在 Resources 节内部声明为资源。由于样式是一种资源,它们遵循适用于所有资源的相同范围规则,因此样式的声明位置将影响它的适用范围。例如,如果在应用程序定义XAML 文件的根元素中声明样式,则该样式可在应用程序中的任何位置使用。如果您在创建导航应用程序时在该应用程序的一个XAML 文件中声明了样式,则该样式只能在该XAML 文件中使用。有关资源范围规则的更多信息,请参见资源概述

样式声明由 Style 对象构成,该对象包含由一个或多个 Setter 对象组成的集合。每个 Setter 都包含一个 Property 和一个 Value。属性是样式所应用于的元素的属性名。将样式声明为资源后,就可以像引用任何其他资源一样引用样式。

使用 Windows Presentation Foundation (WPF) 样式和模板化模型不仅可以维护和共享外观,还可以让呈现与逻辑分离。样式和模板化模型包括一套允许您自定义UI 的功能。这套功能包括 Style 类以及下列组件:

下面代码演示了 将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 

注意 sytle 的作用域 TargetType指定了style要适用到的“类型”,我们可以在这里指向控件。如果一个style没有 x:key 标记,那么它将适用在它的作用域下的所有控件类型。演示代码第23行和42行演示了 显式指定一个style的用法,值得一提的是,“如果显式的为一个控件指定了style ,那么 这个style具有更高的优先权”。本例中第42行的button仅仅作用了一个style,全局的(第6行声明的)style对它无效。

style的强大之处在这里体现,他可以更改一个控件的ControlTemplate,本例中 将一个矩形的button变成了椭圆。

style的trigger在上一篇文章里已经用到,本例就不再演示。

本节完

待续ing...

原文地址:https://www.cnblogs.com/vir56k/p/1935703.html