C# 什么时候应该在 WPF 中使用依赖属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18592630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When should I use dependency properties in WPF?
提问by user2381422
When should I use dependency properties in WPF?
什么时候应该在 WPF 中使用依赖属性?
They are static, so we save a lot on memory, compared to using .NET properties. Other gains of using dependency properties over .NET properties are: 1) no need to check thread access 2) prompt a containing element to be rendered etc...
它们是静态的,因此与使用 .NET 属性相比,我们可以节省大量内存。使用依赖属性而不是 .NET 属性的其他好处是:1) 无需检查线程访问 2) 提示要呈现包含元素等...
So it seems I should ALWAYS use dependency properties in my projects where I use WPF?
所以看起来我应该总是在我使用 WPF 的项目中使用依赖属性?
Maybe for some trivial properties of helper classes here and there I could get away with .NET properties...
也许对于这里和那里的辅助类的一些微不足道的属性,我可以摆脱 .NET 属性......
采纳答案by Nitin
Dependency Property is a broad concept to explain which might take couple of pages to write. So just to answer your main question, Dependency property is used where
依赖属性是一个广泛的概念,用于解释可能需要几页才能编写的内容。所以只是为了回答你的主要问题,依赖属性用于
You know the property is going to be the binding target i.e you are creating a user control/custom control and want property that should be binding driven.
You want automatic property change notifications (coerse and validation also).
We want value inheritance in styles,themes, parent or default value.
There is not need to create the properties on Model or ViewModel layer as dependency properties most of the time as that is not going to help much on memory saving front as most of the properties we define in model/VM will have values per instance as they will be constantly changing. Resolving Dependency property value is a burden in itself so making property dependency unnecessarily is not advisable.
您知道该属性将成为绑定目标,即您正在创建一个用户控件/自定义控件并希望属性应该是绑定驱动的。
您需要自动的属性更改通知(也可以是 coerse 和验证)。
我们希望在样式、主题、父级或默认值中继承值。
大多数时候不需要在 Model 或 ViewModel 层上创建属性作为依赖属性,因为这对节省内存没有太大帮助,因为我们在模型/VM 中定义的大多数属性将具有每个实例的值,因为它们将不断变化。解析 Dependency 属性值本身就是一种负担,因此不建议不必要地进行属性依赖。
Thanks
谢谢
回答by Ahmed Ahmed
The main difference is, that the value of a normal .NET property is read directly from a private member in your class, whereas the value of a DependencyProperty is resolved dynamically when calling the GetValue() method that is inherited from DependencyObject.
主要区别在于,普通 .NET 属性的值是直接从类中的私有成员读取的,而 DependencyProperty 的值是在调用从 DependencyObject 继承的 GetValue() 方法时动态解析的。
When you set a value of a dependency property it is not stored in a field of your object, but in a dictionary of keys and values provided by the base class DependencyObject. The key of an entry is the name of the property and the value is the value you want to set.
当您设置依赖属性的值时,它不会存储在对象的字段中,而是存储在基类 DependencyObject 提供的键和值的字典中。条目的键是属性的名称,值是您要设置的值。
The advantages of dependency properties are
依赖属性的优点是
- Reduced memory footprint:
It's a huge dissipation to store a field for each property when you think that over 90% of the properties of a UI control typically stay at its initial values. Dependency properties solve these problems by only store modified properties in the instance. The default values are stored once within the dependency property. - Value inheritance:
When you access a dependency property the value is resolved by using a value resolution strategy. If no local value is set, the dependency property navigates up the logical tree until it finds a value. When you set the FontSize on the root element it applies to all textblocks below except you override the value. - Change notification:
Dependency properties have a built-in change notification mechanism. By registering a callback in the property metadata you get notified, when the value of the property has been changed. This is also used by the databinding.
- 减少内存占用:
当您认为 UI 控件的 90% 以上的属性通常保持其初始值时,为每个属性存储一个字段是一种巨大的消耗。依赖属性通过仅在实例中存储修改的属性来解决这些问题。默认值在依赖属性中存储一次。 - 值继承:
当您访问依赖项属性时,通过使用值解析策略来解析值。如果没有设置本地值,则依赖属性会向上导航逻辑树,直到找到一个值。当您在根元素上设置 FontSize 时,它适用于下面的所有文本块,除非您覆盖该值。 - 变更通知:
依赖属性有一个内置的变更通知机制。通过在属性元数据中注册回调,您会在属性值更改时收到通知。这也被数据绑定使用。
check the below url for more details about the magic behid it
查看下面的网址,了解有关隐藏它的魔法的更多详细信息
回答by Will Eddins
Dependency properties are used when you want data binding in a UserControl
, and is the standard method of data binding for the WPF Framework controls. DPs have slightly better binding performance, and everything is provided to you when inside a UserControl
to implement them.
依赖属性用于在 中进行数据绑定UserControl
,并且是 WPF 框架控件的标准数据绑定方法。DPs 的绑定性能稍好一些,UserControl
在实现它们的时候,所有的东西都提供给你。
Otherwise, you typically use INotifyPropertyChanged
for binding elsewhere, since it's easier to implement in stand-alone classes and has less overhead. As far as your original assumptions:
否则,您通常INotifyPropertyChanged
在别处使用for 绑定,因为它更容易在独立类中实现并且开销更少。至于你原来的假设:
- There is a local instance of your variable, you definitely do not save any overhead over a property since there is significant data binding logic built-in.
- They must be accessed on the main thread.
- 您的变量有一个本地实例,您绝对不会节省任何属性开销,因为内置了重要的数据绑定逻辑。
- 它们必须在主线程上访问。
回答by Benoit Blanchon
The main reason to create DependencyProperty
is when you write you own WPF control.
DependencyProperties
can be used as binding source and target, and can be animated.
The properties of all framework's controls are implemented as DependencyProperty
, that why you can make powerful data binding in XAML.
创建的主要原因DependencyProperty
是当您编写自己的 WPF 控件时。
DependencyProperties
可以用作绑定源和目标,并且可以进行动画处理。所有框架控件的属性都实现为DependencyProperty
,这就是为什么您可以在 XAML 中进行强大的数据绑定。
However in most situation, like in with the MVVM pattern, you don't need dependency properties, INotifyPropertyChanged
is enough.
然而,在大多数情况下,就像在 MVVM 模式中一样,您不需要依赖属性INotifyPropertyChanged
就足够了。
回答by Sheridan
Perhaps you should take another look at the Dependency Properties Overviewpage at MSDN.
也许您应该再看看MSDN上的依赖属性概述页面。
Personally, I would only ever create a DependencyProperty
when I really needto. For the most part, I use normal CLR properties in data type and view model classes to bind to... this is absolutely fine, as long as I implement the INotifyPropertyChanged
interface.
就个人而言,我只会DependencyProperty
在真正需要的时候创建一个。在大多数情况下,我在数据类型和视图模型类中使用普通的 CLR 属性来绑定到……这绝对没问题,只要我实现了INotifyPropertyChanged
接口。
So for all of my usual data bindings, I use normal CLR properties. I onlydeclare a Dependency Property
in order to provide some added functionality in a UserControl
.
所以对于我所有常用的数据绑定,我使用普通的 CLR 属性。我只声明 aDependency Property
以便在UserControl
.
回答by Sheridan
CLR Property vs. Dependency Property
CLR 属性与依赖属性
A CLR property reads directly from the private member of the class. The Get() and Set() methods of the class retrieve and store the values of the property. Whereas when you set a value of a Dependency Property it is not stored in a field of your object, but in a dictionary of keys and values provided by the base class DependencyObject. The key of an entry is the name of the property and the value is the value you want to set.
CLR 属性直接从类的私有成员中读取。该类的 Get() 和 Set() 方法检索和存储属性的值。而当您设置依赖属性的值时,它并不存储在对象的字段中,而是存储在基类 DependencyObject 提供的键和值的字典中。条目的键是属性的名称,值是您要设置的值。
Advantages of a Dependency Property Less memory consumption
依赖属性的优点 更少的内存消耗
The Dependency Property stores the property only when it is altered or modified. Hence a huge amount of memory for fields are free.
依赖属性仅在更改或修改属性时存储该属性。因此,大量的字段内存是免费的。
Property value inheritanceIt means that if no value is set for the property then it will return to the inheritance tree up to where it gets the value.
属性值继承这意味着如果没有为属性设置值,那么它将返回到继承树直到它获取值的位置。
Change notification and Data BindingsWhenever a property changes its value it provides notification in the Dependency Property using INotifyPropertyChange and also helps in data binding.
更改通知和数据绑定每当属性更改其值时,它都会使用 INotifyPropertyChange 在依赖属性中提供通知,并且还有助于数据绑定。
Participation in animation, styles and templatesA Dependency Property can animate, set styles using style setters and even provide templates for the control.
参与动画、样式和模板依赖属性可以设置动画、使用样式设置器设置样式,甚至为控件提供模板。
CallBacksWhenever a property is changed you can have a callback invoked.
回调每当更改属性,你可以有一个回调调用。
ResourcesYou can define a Resource for the definition of a Dependency Property in XAML.
资源您可以为 XAML 中的依赖属性定义定义一个资源。
Overriding MetadataYou can define certain behaviours of a Dependency Property using PropertyMetaData. Thus, overriding a metadata from a derived property will not require you to redefine or re-implement the entire property definition.
覆盖元数据您可以使用 PropertyMetaData 定义依赖属性的某些行为。因此,从派生属性覆盖元数据不需要您重新定义或重新实现整个属性定义。