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.触发器
    待补充
  • 相关阅读:
    矩阵补全(Matrix Completion)和缺失值预处理
    【机器学习基础】对 softmax 和 cross-entropy 求导
    Mendeley使用小技巧
    [Active Learning] Multi-Criteria-based Active Learning
    回归树(Regression Tree)
    Typora + Mathpix Snip,相见恨晚的神器
    【机器学习之数学】02 梯度下降法、最速下降法、牛顿法、共轭方向法、拟牛顿法
    【机器学习之数学】01 导数、偏导数、方向导数、梯度
    “卷积神经网络(Convolutional Neural Network,CNN)”之问
    一篇带你完全掌握线程的博客
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1214234.html
Copyright © 2011-2022 走看看