构成Style最重要的两种元素是Setter和Trigger,Setter类帮助我们设置控件的静态外观风格,Trigger类则帮助我们设置控件的行为风格。
Style作用:
为了设计多种外观形式和动作行为。
Style中的Setter
Setter设置器,Setter类的Property属性用来指明你想为目标的哪个属性赋值:Setter类的Value属性则是你提供的属性值。
下面的例子中在Window的资源字典中放置一个针对TextBlock的Style,Style中使用若干个Setter来设定TextBLock的一些属性,这样程序中的TextBlock就会具有统一的风格,除非你使用{x:Null}清空Style样式。
XAML代码如下:
<Window x:Class="WpfStyle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="250" Width="300">
<Window.Resources>
<Style TargetType="TextBlock">
<Style.Setters>
<Setter Property="FontSize" Value="24"/>
<Setter Property="TextDecorations" Value="Underline"/>
<Setter Property="FontStyle" Value="Italic"/>
</Style.Setters>
</Style>
</Window.Resources>
<StackPanel>
<TextBlock Text="Hello WPF!"/>
<TextBlock Text="这是一个TextBlock"/>
<TextBlock Text="这也是一个TextBlock" Style="{ x:Null}"/>
</StackPanel>
</Window