效果如下:
案例一描述:
TargetType 设置所有TextBlock样式
<s:SurfaceWindow.Resources>
<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" />
<Setter Property="Background" Value="Yellow"/>
</Style>
</s:SurfaceWindow.Resources>
<Grid>
<TextBlock >你好!</TextBlock>
</Grid>
案例二描述:
通过key-StaticResource方式获取style中设置的参数:
注意:不同于TargetType的是key-StaticResource 必须用“Control.FontFamily”的方式来设置。
<s:SurfaceWindow.Resources>
<Style x:Key="style1">
<Setter Property="Control.FontFamily" Value="Segoe Black" />
<Setter Property="Control.HorizontalAlignment" Value="Center" />
<Setter Property="Control.FontSize" Value="12pt" />
<Setter Property="Control.Foreground" Value="#777777" />
<Setter Property="Control.Background" Value="Yellow"/>
</Style>
</s:SurfaceWindow.Resources>
<Grid>
<Label Content="你好!" Style="{StaticResource style1}"></Label>
</Grid>