`

wpf - default implicit style

    博客分类:
  • WPF
wpf 
阅读更多

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

    }

 

分享到:
评论

相关推荐

    bootstrap-wpf-style-master 样式

    Bootstrap-WPF 样式是一种将流行的前端框架 Bootstrap 的设计风格应用于 WPF(Windows Presentation Foundation)应用程序的方法。Bootstrap 是一个广泛使用的开源工具包,主要用于构建响应式、移动设备优先的网页...

    通用WPF主题控件包rubyer-wpf-master

    通用WPF主题控件包rubyer-wpf-master是一款专为Windows Presentation Foundation (WPF) 应用程序设计的开源UI框架。它提供了丰富的主题和控件,旨在帮助开发者快速构建美观且用户友好的应用程序界面。在2.0.0版本中...

    gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架.zip

    gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...

    WPF - TabControl - Style用法

    学习在WPF当中如何使用Style定制TabControl的样式,包含TabControl的页面选项切换的简单动画效果。原始代码网上找的,我改了一些代码。

    wpf-mvvm-DeskTop-Sample-master_C#_WPF_wpf客户端zfs_

    标题中的“wpf-mvvm-DeskTop-Sample-master”表明这是一个关于WPF(Windows Presentation Foundation)桌面应用程序的示例项目,使用了MVVM(Model-View-ViewModel)设计模式。这个项目是用C#编程语言编写的,面向的...

    WPF-Blockly-master.zip

    **WPF-Blockly** 是一个基于Windows Presentation Foundation (WPF) 的图形化编程工具,它为用户提供了构建和设计程序的直观界面。WPF作为Microsoft .NET Framework的一部分,主要用于构建桌面应用程序,它提供了...

    bootstrap-wpf-style.zip

    标题"bootstrap-wpf-style.zip"暗示了这个压缩包包含了一个项目或资源集合,使得开发者能够在WPF应用中应用Bootstrap的样式。这可能是通过移植Bootstrap的CSS规则到WPF的样式系统,或者创建一个与Bootstrap类似的...

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 4

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手

    WPF-ControlBase-master.zip

    在这个名为"WPF-ControlBase-master.zip"的压缩包中,我们可以推测它包含了一个基于WPF的控制库项目,可能是一个开源或者个人开发的项目,用于提供自定义的WPF控件。这些控件可能是对标准WPF控件的扩展或增强,也...

    WPF-Samples-master_WPF基本sample_

    WPF的基本空间历程,使用.net core3.0.1版本

    基于WPF的图形化编程控件和环境WPF-Blockly-master

    【标题】"基于WPF的图形化编程控件和环境WPF-Blockly-master" 提供了一个创新的编程体验,它将传统的代码编写转变为图形化的流程图形式,使得编程变得更加直观和易于理解。WPF(Windows Presentation Foundation)是...

    WPF-Diagram-Designer:WPF图表设计器源代码

    通过深入研究WPF-Diagram-Designer的源代码(如WPF-Diagram-Designer-master文件夹中的内容),开发者不仅可以学习到如何在WPF中构建复杂的图形界面,还可以了解到图形编辑器的设计原理和实现细节,对于提升图形应用...

    AI-wpf-controls一个Wpf控件库

    在本文中,我们将深入探讨"AI-wpf-controls",这是一个专为Windows Presentation Foundation(WPF)框架设计的控件库。这个独特的库整合了多个知名控件库的优点,包括MahApps.Metro、Material-Design、HandyControl...

    WPF-MaterialDesign-master.zip_WPF_WPF非常好的界面_包括多种漂亮的皮肤_漂亮的控件_配色

    在本项目"WPF-MaterialDesign-master.zip"中,重点在于利用**Material Design**这一设计语言来增强WPF应用的视觉效果。Material Design是Google推出的一种设计规范,其灵感来源于现实世界中的纸张和墨水,强调层次感...

    Wpf-glTF-testing.zip

    在“Wpf-glTF-testing.zip”压缩包中,我们有一个基于WPF(Windows Presentation Foundation)的简化glTF文件查看器项目。 WPF是.NET Framework的一部分,是一个用于构建Windows桌面应用程序的框架。它提供了丰富的...

    wpf---StatusBar

    “wpf---StatusBar”这个标题表明我们将探讨的是WPF(Windows Presentation Foundation)框架中的StatusBar组件。WPF是.NET Framework的一部分,用于构建桌面应用程序,它提供了丰富的用户界面(UI)功能。StatusBar...

    WPF-强大的图表.zip

    **WPF - 强大的图表技术** Windows Presentation Foundation (WPF) 是Microsoft开发的一个用于构建桌面应用程序的框架,它提供了丰富的图形系统,包括对2D和3D图形的强大支持。在WPF中,开发人员可以利用各种图表...

    Wpf的Diagram画板aistudio-wpf-diagram-master

    【标题】"Wpf的Diagram画板aistudio-wpf-diagram-master" 是一个基于WPF(Windows Presentation Foundation)技术的图形设计工具,用于创建和编辑图表或流程图。这个项目是在原有的WPF-Diagram-Designer基础上进行的...

    OpenControls.Wpf-master

    《OpenControls.Wpf-master:深度探索WPF框架控件》 在Windows Presentation Foundation(WPF)的世界里,开发者们能够创建出美观且功能丰富的桌面应用程序。OpenControls.Wpf-master项目,正如其名,是一个专注于...

    wpf-datagrid-access DB

    在这个“wpf-datagrid-access DB”主题中,我们将深入探讨如何利用WPF Datagrid与Microsoft Access数据库进行交互,实现数据的读取、更新和保存。 1. **WPF Datagrid简介** - Datagrid是WPF中的一个数据展示控件,...

Global site tag (gtag.js) - Google Analytics