WPF基础入门
Class2:样式
1、内联样式:优先度最高
<Grid>
<StackPanel>
<!--内联样式优先度高-->
<Button
Background="Red"
Height="10"
Width="100"
FontSize="20"
Content="SB">
</Button>
</StackPanel>
</Grid>
2、创建公共Style样式资源(eg.前端的css)
<Window.Resources>
<!--创建资源-->
<!--TargetType类型需要和使用的控件一致-->
<Style x:Key="style_1" TargetType="Button">
<Setter Property="FontSize" Value="18"></Setter>
<Setter Property="Foreground" Value="Blue"></Setter>
<Setter Property="Background" Value="DimGray"></Setter>
<Setter Property="Height" Value="40"></Setter>
<Setter Property="Content" Value="BaseBtn"></Setter>
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<Button Style="{StaticResource style_1}"></Button>
</StackPanel>
</Grid>
3、基于Style,继承资源(Style2继承Style1)
<Window.Resources>
<!--创建资源-->
<!--TargetType类型需要一致-->
<Style x:Key="style_1" TargetType="Button">
<Setter Property="FontSize" Value="18"></Setter>
<Setter Property="Foreground" Value="Blue"></Setter>
<Setter Property="Background" Value="DimGray"></Setter>
<Setter Property="Height" Value="40"></Setter>
<Setter Property="Content" Value="BaseBtn"></Setter>
</Style>
<Style x:Key="child" TargetType="Button" BasedOn="{StaticResource style_1}">
<Setter Property="Content" Value="继承资源"></Setter>
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<Button Style="{StaticResource style_1}"></Button>
<Button Style="{StaticResource child}"></Button>
</StackPanel>
</Grid>
效果图: