wpf - default implicit style

本文探讨了WPF中样式定义的默认值处理方式。详细分析了如何通过x:Key属性来设置样式,并解释了当省略x:Key时如何应用默认样式。此外,还通过代码示例展示了如何查找并验证默认TextBox样式的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

We know that if you left out the x:Key property on the style definition, we get an implicit style, which will be applied to all control of the TargetType unless explicitly specified. However, do you know something under the hood?

 

we know there are generally two ways to deal with some default value, one is to treat special values, and the other is a smart default; in our case, the x:Key property is used as the key to one dictionary, so which means  you cannot left x:Null as the Key. so it must be suing the smart deafult. 

 

What is the default value? suppose that we are dealing with default style for a TextBox, will the deafult key simply as "TextBox", that is too easy to conflict with others, because anyone can make a simple stirng easily.  so code 

       <Style x:Key="TextBox" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Blue" />
    </Style>

 is probably not what you meant for. 

 

actually if you write 

    <Style x:Key="System.Windows.Controls.TextBox" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Green" />
    </Style>

 this is equivalent to the following.. 

    <Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Red" />
    </Style>

 

so that the type itself became the key to default key ..

 

However, you may wonder if you can write the Text representation of the Style like this: 

    <Style x:Key="System.Windows.Controls.TextBox" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Green" />
    </Style>

 

No, you CANNOT..  It has no effect. 

 

In fact, you can further verify that by the following code. ( you will need to have defined a implicit style with a setter to set the BorderBrush to Red) 

 

public Style FindDefaultTextBoxStyle()
        {
            var style = TextBox1.FindResource(typeof (TextBox)) as Style;

            if (style != null)
            {
                Console.WriteLine("FindResource(typeof(TextBox) Successfully");
            }
            else
            {
                Console.WriteLine("FindResource(typeof(TextBox) Failed");
            }
            Debugger.Break();
//            var setter = style.Setters[0] as System.Windows.Setter;
//            if (setter != null)
//            {
//                setter.Property
//            }
            Debug.Assert(style.Setters.First(
                p => { 
                var s = p as Setter; 
                if (s != null && s.Property == TextBox.BorderBrushProperty && s.Value == Brushes.Red)
                    return true;
                                                      return false;
                    }
                ) != null);
            return style;
        }

 

 

Attached is the code:

 

the xaml in the ResourceDictionary1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    
    <Style x:Key="TextBox" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Blue" />
    </Style>
    
    <!-- This is how you can define some default style for a control -->
    <Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Red" />
    </Style>

    <!--this is equivalent of wring as below.. -->
<!--    <Style  TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Blue" />
    </Style>-->

    <!--It is not necessary the same as the following... -->
    <Style x:Key="System.Windows.Controls.TextBox" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Green" />
    </Style>
</ResourceDictionary>

 

 

and the code in the MainWindow.xaml.cs

 

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            TextBlock1.Text = typeof (TextBox).ToString();
            FindDefaultTextBoxStyle();
        }


        public Style FindDefaultTextBoxStyle()
        {
            var style = TextBox1.FindResource(typeof (TextBox)) as Style;

            if (style != null)
            {
                Console.WriteLine("FindResource(typeof(TextBox) Successfully");
            }
            else
            {
                Console.WriteLine("FindResource(typeof(TextBox) Failed");
            }
            Debugger.Break();
//            var setter = style.Setters[0] as System.Windows.Setter;
//            if (setter != null)
//            {
//                setter.Property
//            }
            Debug.Assert(style.Setters.First(
                p => { 
                var s = p as Setter; 
                if (s != null && s.Property == TextBox.BorderBrushProperty && s.Value == Brushes.Red)
                    return true;
                                                      return false;
                    }
                ) != null);
            return style;
        }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值