且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是依赖属性?它的用途是什么?

更新时间:2021-10-08 03:37:50

依赖属性:由 DependencyProperty.

Dependency property: A property that is backed by a DependencyProperty.

依赖属性的目的是提供一种方法来基于其他输入的值来计算属性的值.

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.

它具有以下功能:

  • 可以在样式中设置该属性.

  • The property can be set in a style.

属性可以通过数据绑定设置.

The property can be set through data binding.

可以使用动态资源引用来设置该属性.

The property can be set with a dynamic resource reference.

该属性可以自动从元素树中的父元素继承其值.

The property can inherit its value automatically from a parent element in the element tree.

该属性可以设置动画.

该属性可以在属性的先前值发生更改时报告,并且可以强制该属性值.

The property can report when the previous value of the property has been changed and the property value can be coerced.

属性向 WPF 报告信息,例如更改属性值是否应要求布局系统重新组合元素的视觉效果.

The property reports information to WPF, such as whether changing a property value should require the layout system to recompose the visuals for an element.

该属性在 Visual Studio 的 WPF 设计器中获得支持.例如,可以在属性"窗口中编辑该属性.

The property receives support in the WPF Designer for Visual Studio. For example, the property can be edited in the Properties window.

(内容取自 MSDN)