简体   繁体   English

什么时候应该在 WPF 中使用依赖属性?

[英]When should I use dependency properties in WPF?

When should I use dependency properties in WPF?什么时候应该在 WPF 中使用依赖属性?

They are static, so we save a lot on memory, compared to using .NET properties.它们是静态的,因此与使用 .NET 属性相比,我们可以节省大量内存。 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 属性的其他好处是: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 属性......

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所以只是为了回答你的主要问题,依赖属性用于

  1. You know the property is going to be the binding target ie you are creating a user control/custom control and want property that should be binding driven.您知道该属性将成为绑定目标,即您正在创建一个用户控件/自定义控件并希望属性应该是绑定驱动的。

  2. You want automatic property change notifications (coerse and validation also).您需要自动属性更改通知(也可以是 coerse 和验证)。

  3. We want value inheritance in styles,themes, parent or default value.我们希望在样式、主题、父级或默认值中继承值。

  4. 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.大多数情况下不需要在 Model 或 ViewModel 层上创建属性作为依赖属性,因为这对节省内存没有太大帮助,因为我们在模型/VM 中定义的大多数属性将具有每个实例的值,因为它们将不断变化。 Resolving Dependency property value is a burden in itself so making property dependency unnecessarily is not advisable.解析 Dependency 属性值本身就是一种负担,因此不建议不必要地进行属性依赖。

Thanks谢谢

The main reason to create DependencyProperty is when you write you own WPF control.创建DependencyProperty主要原因是当您编写自己的 WPF 控件时。 DependencyProperties can be used as binding source and target, and can be animated. DependencyProperties可以用作绑定源和目标,并且可以设置动画。 The properties of all framework's controls are implemented as DependencyProperty , that why you can make powerful data binding in XAML.所有框架控件的属性都实现为DependencyProperty ,这就是为什么您可以在 XAML 中进行强大的数据绑定。

However in most situation, like in with the MVVM pattern, you don't need dependency properties, INotifyPropertyChanged is enough.但是在大多数情况下,就像在 MVVM 模式中一样,您不需要依赖属性, INotifyPropertyChanged就足够了。

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.当您设置依赖属性的值时,它不会存储在对象的字段中,而是存储在基类 DependencyObject 提供的键和值的字典中。 The key of an entry is the name of the property and the value is the value you want to set.条目的键是属性的名称,值是您要设置的值。

The advantages of dependency properties are依赖属性的优点是

  1. 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.当您认为 UI 控件的 90% 以上的属性通常保持其初始值时,为每个属性存储一个字段是一种巨大的消耗。 Dependency properties solve these problems by only store modified properties in the instance.依赖属性通过仅在实例中存储修改的属性来解决这些问题。 The default values are stored once within the dependency property.默认值在依赖属性中存储一次。
  2. 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.当您在根元素上设置 FontSize 时,它​​适用于下面的所有文本块,除非您覆盖该值。
  3. 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.这也被数据绑定使用。

check the below url for more details about the magic behid it查看下面的网址,了解有关隐藏它的魔法的更多详细信息

dependency properties in WPF WPF 中的依赖属性

在此处输入图片说明

CLR Property vs. Dependency Property CLR 属性与依赖属性

A CLR property reads directly from the private member of the class. CLR 属性直接从类的私有成员中读取。 The Get() and Set() methods of the class retrieve and store the values of the property.该类的 Get() 和 Set() 方法检索和存储属性的值。 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.而当您设置依赖属性的值时,它并不存储在对象的字段中,而是存储在基类 DependencyObject 提供的键和值的字典中。 The key of an entry is the name of the property and the value is the value you want to set.条目的键是属性的名称,值是您要设置的值。

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 inheritance It 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 Bindings Whenever 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 templates A Dependency Property can animate, set styles using style setters and even provide templates for the control.参与动画、样式和模板依赖属性可以设置动画、使用样式设置器设置样式,甚至为控件提供模板。

CallBacks Whenever a property is changed you can have a callback invoked.每当更改属性,你可以有一个回调回调调用。

Resources You can define a Resource for the definition of a Dependency Property in XAML.资源您可以为 XAML 中的依赖属性定义定义一个资源。

Overriding Metadata You can define certain behaviours of a Dependency Property using PropertyMetaData.覆盖元数据您可以使用 PropertyMetaData 定义依赖属性的某些行为。 Thus, overriding a metadata from a derived property will not require you to redefine or re-implement the entire property definition.因此,从派生属性覆盖元数据不需要您重新定义或重新实现整个属性定义。

Perhaps you should take another look at the Dependency Properties Overview page at MSDN.也许您应该再看看 MSDN 上的依赖属性概述页面。

Personally, I would only ever create a DependencyProperty when I really need to.就个人而言,我只会在真正需要时创建DependencyProperty 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.在大多数情况下,我在数据类型和视图模型类中使用普通的 CLR 属性来绑定到......这绝对没问题,只要我实现INotifyPropertyChanged接口。

So for all of my usual data bindings, I use normal CLR properties.所以对于我所有常用的数据绑定,我使用普通的 CLR 属性。 I only declare a Dependency Property in order to provide some added functionality in a UserControl .声明了一个Dependency Property ,以便在UserControl提供一些附加功能。

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.依赖属性用于在UserControl数据绑定,并且是 WPF 框架控件的标准数据绑定方法。 DPs have slightly better binding performance , and everything is provided to you when inside a UserControl to implement them. DP 的绑定性能稍好一些,并且在UserControl为您提供所有内容以实现它们。

Otherwise, you typically use INotifyPropertyChanged for binding elsewhere, since it's easier to implement in stand-alone classes and has less overhead.否则,您通常使用INotifyPropertyChanged在别处进行绑定,因为它更容易在独立类中实现并且开销更少。 As far as your original assumptions:至于你原来的假设:

  1. 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.您的变量有一个本地实例,您绝对不会节省任何属性开销,因为内置了重要的数据绑定逻辑。
  2. They must be accessed on the main thread .它们必须在主线程上访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM