什么是依赖属性?它有什么用?

发布于 2024-10-25 08:28:17 字数 216 浏览 6 评论 0原文

可能性:
什么是依赖属性?

什么是依赖属性?它与普通财产有何不同?依赖属性的目的是什么?以及为什么使用它、何时使用它?

Possiblity:
What is a dependency property?

What is a dependency property? How does it differ from a normal property? What is the purpose of dependency properties? And why it is used, when it is used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尾戒 2024-11-01 08:28:17

依赖属性:由 依赖属性

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

这些其他输入可能包括系统属性(例如主题和用户首选项)、即时属性确定机制(例如数据绑定和动画/故事板)、多用途模板(例如资源和样式)或通过父级已知的值与元素树中其他元素的子关系。

它具有以下功能:

  • 可以在样式中设置属性。

  • 可以通过数据绑定来设置属性。

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

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

  • 该属性可以设置动画。

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

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

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

(内容取自 MSDN)

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.

It has Following capabilities:

  • 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 be animated.

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

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

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

(Content taken from MSDN)

半世晨晓 2024-11-01 08:28:17

依赖属性将其值存储在类之外,因此属性可以分配给一个对象,而不必更改该对象的类。它们还支持 WPF 中常见的情况,其中对象可能具有非常多的属性,但只有少数属性具有非默认值。依赖属性可以有默认值,因此这可以减少内存使用量。还有更多内容,请阅读 MSDN 上的文章:依赖属性概述

Dependency properties store their values outside the class, so properties can be assigned to an object without having to change the object's class. They also support a situation common in WPF where an object may have very many properties, but only a few have non-default values. Dependency properties can have default values, so this reduces memory usage. There is lots more, read the article: Dependency Properties Overview on MSDN.

寄居者 2024-11-01 08:28:17

我认为 MSDN 文章 可以为您提供更多信息。

据我所知,依赖属性依赖于其他值。

依赖属性的用途
是提供一种计算方法
基于价值的财产价值
其他输入。
这些其他输入
可能包括系统属性,例如
作为主题和用户偏好,
即时财产确定
数据绑定等机制
动画/故事板,多用途
模板,例如资源和
风格,或通过已知的价值观
与他人的亲子关系
元素树中的元素。在
另外,依赖属性可以是
实施以提供独立的
验证、默认值、回调
监控其他的变化
属性,以及一个可以
强制属性值基于
潜在的运行时信息。
派生类也可以改变一些
的具体特征
通过覆盖现有属性
依赖属性元数据,而不是
而不是覆盖实际的
现有属性的实施
或创建新属性。

I think the MSDN article can give you more information.

From what I read is that a Dependency Property relies on other values.

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. In
addition, a dependency property can be
implemented to provide self-contained
validation, default values, callbacks
that monitor changes to other
properties, and a system that can
coerce property values based on
potentially runtime information.
Derived classes can also change some
specific characteristics of an
existing property by overriding
dependency property metadata, rather
than overriding the actual
implementation of existing properties
or creating new properties.

少年亿悲伤 2024-11-01 08:28:17

如果您正在谈论具有 Dependency 属性(如 Microsoft.Practices.Unity 中定义)

[Dependency]
public string MyProperty { get; set; }

然后,Unity 框架将其用于依赖注入 (DI)。基本上,属性值是由 DI 框架在运行时设置的,而不是直接在代码中设置。

但是,还有另一个 Dependency 属性定义在System.Runtime.CompilerServices。请您更新您的问题,您指的是哪一个。

If you are talking about a property with a Dependency attribute (as defined in Microsoft.Practices.Unity)

[Dependency]
public string MyProperty { get; set; }

Then this is used for dependency injection (DI) by the Unity framework. Basically, the property value is set at runtime by the DI framework, rather than being set directly in your code.

However, there is another Dependency attribute defined in System.Runtime.CompilerServices. Please could you update your question with which one you mean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文