zoukankan      html  css  js  c++  java
  • WPF style原则问题

    就近原则

    1、“行内”覆盖“嵌入”,“嵌入”覆盖“外部”
    Style.xml

    <Window.Resources>   
        <Grid.Resources>   
    ….中间层
            <Button.Resources>   
            </Button.Resources>  
        </Grid.Resources> 
    </Window.Resources>     
    

    举例

    下例中,Button显示Coral的颜色,如果去掉Coral,则显示Chartreuse,去掉行内和嵌入,才显示Aqua
    Coral>Chartreuse>Aqua

    <Grid>
        <Grid.Resources>
            <!--外部-->
            <Style TargetType="Button">
                <Setter Property="Background" Value="Aqua"></Setter>
            </Style>
        </Grid.Resources>
        <!--行内-->
        <Button Background="Coral">
            <Button.Resources>
                <!--嵌入-->
                <Style TargetType="Button">
                    <Setter Property="Background" Value="Chartreuse"></Setter>
                </Style>
            </Button.Resources>
        </Button>
    </Grid>
    

    给外部资源命名,并在行内引用,则此时外部->行内
    Coral>Aqua>Chartreuse

    <Grid>
        <Grid.Resources>
            <!--外部以行内的形式存在-->
            <Style TargetType="Button" x:Key="ButtonStyle">
                <Setter Property="Background" Value="Aqua"></Setter>
            </Style>
        </Grid.Resources>
        <!--行内-->
        <Button Background="Coral" Style="{StaticResource ButtonStyle}">
            <Button.Resources>
                <!--嵌入-->
                <Style TargetType="Button">
                    <Setter Property="Background" Value="Chartreuse"></Setter>
                </Style>
            </Button.Resources>
        </Button>
    </Grid>
    

    2、同级别遵循“就近”

    引用原则

    如果没有给Style命名,则默认所有该范围下的目标控件都使用该Style
    如果一个控件有多个Style,则根据就近原则来判定用什么Style
    如果Style有命名(x:Key),则需要在控件中引用Style(eg.Style="{StaticResource ButtonStyle}")才会使用该Style

    同一控件只能设置一次Resources

     

    示例代码

    https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/Style 的StyleDemo和StyleDemo2

  • 相关阅读:
    搞定 Linux Shell 文本处理工具,看完这篇还不够~
    ARM 版的Clang的使用
    GDB入门学习之gef插件的使用
    mac使用apktool
    python实现md5
    fridahookjava
    js hook array对象的push方法
    app逆向java转python代码
    python合并两个有序数组
    MySQL update 语句加锁分析
  • 原文地址:https://www.cnblogs.com/Lulus/p/8157360.html
Copyright © 2011-2022 走看看