wpf 样式

wpf 样式

wpf的样式与html中的css作用基本一致,都是用作样式重用的

样式使用的标签是 Window.Resources ,样式标签是 Window 标签的子标签

样式的命名使用: x:Key="样式名称" 

使用样式使用 {StaticResource 样式名称} 或者 {DynamicResource 样式名称} 

  • StaticResource 静态样式
  • DynamicResource 动态样式

静态样式在程序启动时候就固定了,动态样式可以在程序启动后用代码修改样式

<Window>
    <Window.Resources>
        <LinearGradientBrush x:Key="brush1">
            <GradientStop Offset="0.3" Color="Yellow"/>
            <GradientStop Offset="0.7" Color="Brown"/>
        </LinearGradientBrush>
    </Window.Resources>
    <StackPanel>        
        <Rectangle Height="100" Stroke="Pink" StrokeThickness="20" Fill="{StaticResource brush1}"/>        
        <Button x:Name="button1" Content="Replace Brush" Height="100" Click="OnReplaceBrush"/>
    </StackPanel>
</Window>

 

StyleSetter

如果字体写一个标签,字体颜色又写一个标签,代码量会特别大,调用也会特别麻烦,使用Style调用起来会比较方便

 Style 标签的属性

  • TargetType 表示标签类型,如:按钮 文本框...
  • x:Key 标签名称

setter 标签的属性

  • Property 样式
  • Value 样式值
<Style TargetType="Button" x:Key="btn1">
    <Setter Property="Background" Value="Red"></Setter>
</Style>

多层样式

样式可以指定父类,算是继承,子样式继承父样式的所有样式,子样式可以对父级样式进行修改

使用 Style 的 BasedOn 属性设置父级样式

<Style TargetType="Button" x:Key="Btn1">
    <Setter Property="Background" Value="Red"></Setter>
    <Setter Property="FontSize" Value="50px"></Setter>
</Style>
<Style x:Key="Btn2" TargetType="Button" BasedOn="{StaticResource Btn1}">
    <Setter Property="FontSize" Value="10px"></Setter>
</Style>

 

 

posted @ 2020-03-03 15:06  Simian_2018_12_22  阅读(186)  评论(0编辑  收藏  举报