1.样式的使用Style
说明
1.WPF中各类控件元素,都可以自由设置样式
2.字体FontFamily、字体大小FontSize、背景颜色Background、字体颜色Foreground、边距Margin、水平位置HorizontalAlignment、垂直位置VerticalAlignment等
3.样式中可以实现继承的关系,触发器
使用
首先声明
<Window.Resources>
<Style x:Key="defaultstyle">//x:key 唯一地标识作为资源创建和引用且存在于 ResourceDictionary 中的元素。
</Style>
</Window.Resources>
代码演示:
<Window.Resources>
<Style x:Key="defaultstyle" TargetType="Button">
<Setter Property="FontSize" Value="40" ></Setter>
<Setter Property="Background" Value="Pink"></Setter>
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<Button Style="{StaticResource defaultstyle}" Content="hello" Width="200" Height="50"/>
</StackPanel>
</Grid>
注意:其中x:Key=“defaultstyle 与button中的<Button Style=”{StaticResource defaultstyle}"实现两者之间的绑定,静态绑定
代表button使用该样式 如果button中设置属性与style设置的同一属性吗,button自设置优先级大于style的