`

wpf - Dependency Property Value Precedence

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

A dependency property to an DependencyObject may get its value from various source, the type of sources that the DependencyProperty may get include 

 

  • Default
  • DefaultStyle
  • DefaultTrigger
  • Style
  • ...
  • Local
The value  of such sources are identified in those sources BaseValueSource ;

Quote from the MSDN documentation:

Each value of this enumeration indicates that a particular aspect of the overall WPF property system was responsible for the effective value determination of a certain dependency property value on a specific dependency object. To learn more about the WPF property system and the precedence at which each aspect of the property system operates, see Dependency Property Value Precedence.

Leading from the article Dependency Properties Overview , and we followed to the topic of Dependency Property Value Precedence, where explains the working of the Windows Presentation Foundataion (WPF) property system can affect the value of dependency Property, and describes the precedence by which aspects of the property system apply to the effective value of a property.


Below is the official list of rule to determine the precedence of list; I excerpt it here to make it convenietn for user to reference. 

<quote>
The following is the definitive order that the property system uses when assigning the run-time values of dependency properties. Highest precedence is listed first. This list expands on some of the generalizations made in the Dependency Properties Overview.

  1. Property system coercion. For details on coercion, see Coercion, Animation, and Base Value later in this topic.

  2. Active animations, or animations with a Hold behavior. In order to have any practical effect, an animation of a property must be able to have precedence over the base (unanimated) value, even if that value was set locally. For details, see Coercion, Animation, and Base Value later in this topic.

  3. Local value. A local value might be set through the convenience of the "wrapper" property, which also equates to setting as an attribute or property element in XAML, or by a call to theSetValue API using a property of a specific instance. If you set a local value by using a binding or a resource, these each act in the precedence as if a direct value was set.

  4. TemplatedParent template properties. An element has a TemplatedParent if it was created as part of a template (a ControlTemplate or DataTemplate). For details on when this applies, seeTemplatedParent later in this topic. Within the template, the following precedence applies:

    1. Triggers from the TemplatedParent template.

    2. Property sets (typically through XAML attributes) in the TemplatedParent template.

  5. Implicit style. Applies only to the Style property. The Style property is filled by any style resource with a key that matches the type of that element. That style resource must exist either in the page or the application; lookup for an implicit style resource does not proceed into the themes.

  6. Style triggers. The triggers within styles from page or application (these styles might be either explicit or implicit styles, but not from the default styles, which have lower precedence).

  7. Template triggers. Any trigger from a template within a style, or a directly applied template.

  8. Style setters. Values from a Setter within styles from page or application.

  9. Default (theme) style. For details on when this applies, and how theme styles relate to the templates within theme styles, see Default (Theme) Styles later in this topic. Within a default style, the following order of precedence applies:

    1. Active triggers in the theme style.

    2. Setters in the theme style.

  10. Inheritance. A few dependency properties inherit their values from parent element to child elements, such that they need not be set specifically on each element throughout an application. For details see Property Value Inheritance.

  11. Default value from dependency property metadata. Any given dependency property may have a default value as established by the property system registration of that particular property. Also, derived classes that inherit a dependency property have the option to override that metadata (including the default value) on a per-type basis. See Dependency Property Metadata for more information. Because inheritance is checked before default value, for an inherited property, a parent element default value takes precedence over a child element. Consequently, if an inheritable property is not set anywhere, the default value as specified on the root or parent is used instead of the child element default value.

     

</quote>

I would strongly recommend readers to have a close look at the article of Dependency Property Value Precedence.

Below I will show some of the techniques regaring either get the ValueSource, BaseValueSource and etc..

Get ValueSource of an DependencyProperty of an DependencyObject;

    // GetValueSource
    //  get the value reports various metadata property system characteristics of a specific dependency Property on a particular DependencyObject
    public static ValueSource GetValueSource(FrameworkElement item, DependencyProperty prop)
    {
      // what if the item does not have teh item property 
      if (item == null) throw new ArgumentNullException("item");
      if (prop == null) throw new ArgumentNullException("prop");
      // we assume if we cannot get MetaData about a particular DependencyProperty from an FrameworkElement, then 
      // the FrameworkElement does not have that particular property 
      if (prop.GetMetadata(item) == null) throw new ArgumentException(string.Format("item does not have Dependency property {0}", prop.Name));

      ValueSource valueSource = DependencyPropertyHelper.GetValueSource(item, prop);
      return valueSource;
    }
 
and from the ValueSource, you can further inspect the value of BaseValueSource, in additional to other properties such as IsCurrent, IsCoerced, IsAnimated, IsExpression or etc...


    // GetBaseValueSource
    //  get the ValueSource identify the source of a particular dependency value
    public static BaseValueSource GetBaseValueSource(FrameworkElement item, DependencyProperty prop)
    {
      var valueSource = GetValueSource(item, prop);
      return valueSource.BaseValueSource;
    }
 

分享到:
评论

相关推荐

    通用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-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的一部分,主要用于构建桌面应用程序,它提供了...

    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)是...

    bootstrap-wpf-style-master 样式

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

    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-强大的图表.zip

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

    wpf---StatusBar

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

    wpf-datagrid-access DB

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

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

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

    WPF-ScreenData.zip

    超酷WPF数据可视化,全套源程序,非常适合做数据可视化的程序员使用,WPF,XAML。 找了好久的资源,附有很多很详细的插图,是大数据时代不可缺少的可视化学习数据,仅供大家学习使用请勿用做商业用途。与大家一起...

    c#-的WPF---MVVM例子

    在“c#-的WPF---MVVM例子”中,我们可能会看到以下关键概念: 1. **数据绑定**:WPF的强项之一就是其强大的数据绑定功能。通过数据绑定,View中的UI元素可以直接与ViewModel中的属性关联,当ViewModel中的数据改变...

    gong-wpf-dragdrop-develop.rar

    gong-wpf-dragdrop-develop.rar

Global site tag (gtag.js) - Google Analytics