wpf - default implicit style

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;
        }

    }

 

你可能感兴趣的:(WPF)