What is the difference between Property and Dependency Property

前端 未结 5 1181
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 00:42

Dependency properties are created the same way as properties.

Is a dependency property used only while creating a custom control?

5条回答
  •  星月不相逢
    2020-11-30 01:21

    The primary difference between a dependency droperty and a standard clr property is that a dependency property can be the target of a binding. This allows you to tie the value of the property to a value provided by some other object.

    I would suggest that if you are making a custom control or markup extension, you generally want to expose any of its public properties as dependency properties so that the consumer of your control can better manipulate the settings in XAML (without having to do it in code-behind).

    If your property will commonly be the source of a databinding (e.g. providing the Text for a TextBlock), I would recommend using a standard CLR property and having the containing class implement INotifyPropertyChanged.

    Further....

    A dependency property provides functionality that extends the functionality of a property as opposed to a property that is backed by a field. Often, each such functionality represents or supports a specific feature of the overall WPF set of features.

    Resources

    Data binding

    Styles

    Animations

    Metadata overrides

    Property value inheritance

    http://msdn2.microsoft.com/en-us/library/ms752914.aspx

    Hope this helps.

提交回复
热议问题