WPF中的DependencyProperty [英] DependencyProperty in wpf
本文介绍了WPF中的DependencyProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
依赖项属性的优点是什么.在WPF中使用它的地方?
what are the advantages of Dependency Property. where it is used in wpf? sample code?
推荐答案
Each WPF control registers a set of DependencyProperties to the static DependencyProperty class. Each of them consists of a key - that must be unique per type - and a metadata that contain callbacks and a default value.
All types that want to use DependencyProperties must derive from DependencyObject. This baseclass defines a key, value dictionary that contains local values of dependency properties. The key of an entry is the key defined with the dependency property.
When you access a dependency property over its .NET property wrapper, it internally calls GetValue(DependencyProperty) to access the value.
If a local value is available, it reads it directly from the dictionary. If no value is set if goes up the logical tree and searches for an inherited value. If no value is found it takes the default value defined in the property metadata. This sequence is a bit simplified, but it shows the main concept.
这来自 ^ ]:
DependencyProperty是一个属性,其值取决于(或可以取决于)某些其他来源(例如动画,数据绑定,样式或可视树继承).常规属性的值存储在它所属的对象中,而您可以将依赖项属性视为存储在某个地方的数据库中.该数据库实质上由一个字典组成,该字典将(对象,属性)对映射到它们的值,以及哪些属性依赖于其他属性的映射(例如,因此当您更改Panel的DataContext时,它可以通知其中的所有子级面板).
那么,为什么他们将属性值存储在某个地方的魔术数据库中呢?原因如下:
•减少存储空间.向类中添加属性(即使其值为null)也会为该类的每个实例增加4个字节的空间(对于64位进程为8个字节).仅当实例具有值时,DependencyProperty才会占用空间.例如,FrameworkElement具有数十个依赖项属性,其中大多数从未分配值.如果所有这些属性都存储在类中,则每个实例将是数百个字节.相反,每个实例只有大约40个字节.
•启用附加属性.诸如Canvas.Left和Grid.Row之类的属性必须存储在从未听说过Canvas或Grid的对象上,那么您将它们放在哪里?您将它们放在某个地方的数据库中.
•启用自动属性更改.想象一下如何实现样式或属性继承(在父元素上设置字体或数据上下文之类的值并将其值传播到所有子元素的能力).将所有这些存储在数据库中可以使代码全部放在一个地方,而不是针对需要它的每个对象和属性单独实现.
我不确定我是否完全同意这个答案,但我大部分都知道这是正确的.
This is from http://stackoverflow.com/questions/2505234/need-a-short-and-clear-definition-for-dependency-properties[^]:
A DependencyProperty is a property whose value depends (or can depend) on some other source (such as animation, data binding, styles, or visual tree inheritance). A regular property''s value is stored in the object it belongs to, while you can think of a dependency property as being stored in a database somewhere. This database is essentially composed of a dictionary that maps (object, property) pairs to their values, along with a mapping of which properties depend on other properties (e.g. so when you change the DataContext of a Panel, it can notify all the children inside the panel).
So why do they store property values in some magic database somewhere? There are a few reasons:
•It reduces storage space. Adding a property (even if its value is null) to a class adds 4 bytes (8 for a 64-bit process) of space to every instance of the class. A DependencyProperty only takes up space when an instance has a value. For example, a FrameworkElement has dozens of dependency properties, most of which are never assigned values. If all those properties were stored in the class, each instance would be hundreds of bytes. Instead each instance is only about 40 bytes.
•It enables attached properties. Properties like Canvas.Left and Grid.Row have to be stored on objects that have never heard of a Canvas or Grid, so where do you put them? You put them in a database somewhere.
•It enables automatic property changes. Imagine how you would implement something like styles or property inheritance (the ability to set something like a font or data context on a parent element and have its value propagate to all child elements). Having all of this stored in a database makes it so the code is all in one place instead of being implemented separately for each object and property that needs it.
I am not sure I totally agree with the answer, but I know mostly it is right.
The purpose of dependency properties is to provide a way to compute the value of a property based on the value of other inputs. These other inputs might include system properties such as themes and user preference, just-in-time property determination mechanisms such as data binding and animations/storyboards, multiple-use templates such as resources and styles, or values known through parent-child relationships with other elements in the element tree.
So they're like normal properties, but their value can be automatically computed from the values of other properties.
Check this for sample code
http://www.switchonthecode.com/tutorials/wpf-tutorial-introduction-to-dependency-properties
http://www.betterthaneveryone.com/archive/2010/01/24/wpf-silverlight-xaml-and-dependency-properties.aspx
这篇关于WPF中的DependencyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文