When should I use dependency properties in WPF?

前端 未结 6 436
北荒
北荒 2020-12-08 14:51

When should I use dependency properties in WPF?

They are static, so we save a lot on memory, compared to using .NET properties. Other gains of using dependency prope

相关标签:
6条回答
  • 2020-12-08 15:12

    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.

    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.

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

    0 讨论(0)
  • 2020-12-08 15:14

    enter image description here

    CLR Property vs. Dependency Property

    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.

    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.

    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.

    Overriding Metadata You 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.

    0 讨论(0)
  • 2020-12-08 15:14

    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.

    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:

    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.
    0 讨论(0)
  • 2020-12-08 15:17

    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.

    However in most situation, like in with the MVVM pattern, you don't need dependency properties, INotifyPropertyChanged is enough.

    0 讨论(0)
  • 2020-12-08 15:22

    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 i.e 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).

    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. Resolving Dependency property value is a burden in itself so making property dependency unnecessarily is not advisable.

    Thanks

    0 讨论(0)
  • 2020-12-08 15:27

    Perhaps you should take another look at the Dependency Properties Overview page at MSDN.

    Personally, I would only ever create a DependencyProperty when I really need to. 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.

    So for all of my usual data bindings, I use normal CLR properties. I only declare a Dependency Property in order to provide some added functionality in a UserControl.

    0 讨论(0)
提交回复
热议问题