Setter

Note that property name in this example is qualified with the name of the class (Control) because the Style does not specify a TargetType. 如果使用了x:key属性,则需要显示引用该样式,如果没有设置x:key属性,只设置了TargetType属性,则默认会设置该控件类型的所有样式。

<Style TargetType="{x:Type TextBlock}">
  <Setter Property="FontFamily" Value="Segoe Black" />
  <Setter Property="HorizontalAlignment" Value="Center" />
  <Setter Property="FontSize" Value="12pt" />
  <Setter Property="Foreground" Value="#777777" />
</Style>

<Style TargetType="TextBox">
    <Setter Property="FontSize" Value="10pt" />
</Style>

<Style x:Key="BigFontButtonStyle">
    <Setter Property="Button.FontFamily" Value="Times New Roman"></Setter>
    <Setter Property="Button.FontSize" Value="18"></Setter>
    <Setter Property="Button.FontWeight" Value="Bold"></Setter>
</Style>

<Button Name="btnLogin" Style="{StaticResource BigFontButtonStyle}" Grid.Row="1" Grid.Column="5" Command="{Binding LoginCommand}" CommandParameter="{Binding ElementName=passwordBox}" >Login</Button>

Style

<Style  TargetType="{x:Type typeName}"/>  
-or-  
<Style  TargetType="typeName"/>  

删除样式

Style=”{x:Null}”

<Button Name="btnChangePassword" Style="{x:Null}" Grid.Row="1" Grid.Column="4" Command="{Binding ChangePasswordCommand}" CommandParameter="{Binding ElementName=passwordBox}" >Change Password</Button>

触发器

<Style x:Key="BigFontButtonStyle">
    <Style.Setters>
        <Setter Property="Button.FontFamily" Value="Times New Roman"></Setter>
        <Setter Property="Button.FontSize" Value="18"></Setter>
        <Setter Property="Button.FontWeight" Value="Bold"></Setter>
    </Style.Setters>

    <Style.Triggers>
        <Trigger Property="Button.IsMouseOver" Value="True">
            <Setter Property="Button.Foreground" Value="DarkRed"></Setter>
        </Trigger>

        <EventTrigger RoutedEvent="Mouse.MouseEnter">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="FontSize" To="22"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>

        <EventTrigger RoutedEvent="Mouse.MouseLeave">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Duration="0:0:1" Storyboard.TargetProperty="FontSize"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </Style.Triggers>
    
</Style>

TargetType

如果样式使用了Style.TargetType属性,可以在设置属性时不需要在每个设置器中包含类名。

//没有TargetType属性,在Setter中需要包含类名
<ListBox Grid.Row="1" Name="listBox2" SelectionChanged="listBox2_SelectionChanged">
    <ListBox.ItemContainerStyle>
        <Style>
            <Setter Property="ListBoxItem.Background" Value="LightSteelBlue"></Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    <StackPanel>
        <Label>Blue</Label>
    </StackPanel>
    <StackPanel>
        <Label>Black</Label>
    </StackPanel>
</ListBox>

//有TargetType属性,在Setter中不需要包含类名
<ListBox Grid.Row="1" Name="listBox2" SelectionChanged="listBox2_SelectionChanged">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="LightSteelBlue"></Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    <StackPanel>
        <Label>Blue</Label>
    </StackPanel>
    <StackPanel>
        <Label>Black</Label>
    </StackPanel>
</ListBox>