zoukankan      html  css  js  c++  java
  • wpfStyle注意点



    1.定义Style,需要定义x:Key
        <Canvas>
            
    <Canvas.Resources>
                
    <Style x:Key="btnStyle">
                    
    <Setter Property="Button.Background" Value="Red" />
                
    </Style>
            
    </Canvas.Resources>
            
    <Button Style="{StaticResource btnStyle}">Button</Button>
        
    </Canvas>

    用Setter标签定义属性和值(注意这里的Property是Button.Background,而非Background)

    由于Button继承自Control,所以定义成Control.Background也可以,这样的话,所有Control控件都可以共享这个Style,但并不推荐这种做法,比较混乱

    2.当定义的属性无法找到(wpf揭密中说是会忽略的),在vs2008中是会报编译错误的

    3.优先级问题(以本地值为主)

    <Button Style="{StaticResource btnStyle}" Background="Green">Button</Button>

    4.样式继承(会覆盖父级的样式)
            <Canvas.Resources>
                
    <Style x:Key="basebtnStyle">
                    
    <Setter Property="Control.Width" Value="100" />
                    
    <Setter Property="Control.Background" Value="Green" />
                
    </Style>
                
    <Style x:Key="btnStyle" BasedOn="{StaticResource basebtnStyle}">
                    
    <Setter Property="Control.Background" Value="Red" />
                
    </Style>
            
    </Canvas.Resources>

    5.样式限定
    指定TargetType属性,只有Button控件才可以应用此样式,下列Label应用了此样式是错误的
        <StackPanel>
            
    <StackPanel.Resources>
                
    <Style x:Key="btnStyle" TargetType="{x:Type Button}">
                    
    <Setter Property="Control.Background" Value="Green" />
                
    </Style>
            
    </StackPanel.Resources>
            
    <Button Style="{StaticResource btnStyle}">Button</Button>
            
    <Label Style="{StaticResource btnStyle}">Label</Label>
        
    </StackPanel>

    6.隐式样式
    忽略x:Key属性,把样式指向所有目标类型(如同css样式定义了input样式一般)
        <StackPanel>
            
    <StackPanel.Resources>
                
    <Style TargetType="{x:Type Button}">
                    
    <Setter Property="Background" Value="Green" />
                
    </Style>
            
    </StackPanel.Resources>
            
    <Button>Button</Button>
        
    </StackPanel>

    7.触发器
    待补充
  • 相关阅读:
    将python list数据结果存为html
    英文分词对比nltk vs spacy
    dict读取字典的第一个值-python
    分批查询数据库数据存入文件
    jupyter lab中显示gym的游戏动图
    模型融合
    读取csv中某列转为数字,顺序不变
    tensorflow和pytorch中的自定义网络层理解
    【小白刷题之路Day31】leetcode768. 最多能完成排序的块 II (动态规划、单调栈(没弄懂))
    【小白刷题之路Day31】leetcode769. 最多能完成排序的块(滑动窗口法、特殊使用、极致优化)
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1214234.html
Copyright © 2011-2022 走看看