• 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>

     

  • 相关阅读:
    Y2K Accounting Bug(POJ 2586)
    Power of Cryptography(POJ 2109 math )
    codeforces C. Valera and Tubes
    codeforces C. Devu and Partitioning of the Array
    codeforces C. Ryouko's Memory Note
    codeforces C. k-Tree
    codeforces C. Prime Swaps
    codeforces C. Xor-tree
    codeforces B. Prison Transfer
    codeforces C. Sereja and Swaps
  • 原文地址:https://www.cnblogs.com/sunhouzi/p/12400562.html
Copyright © 2020-2023  润新知