需要对“依赖属性”有一个简短而清晰的定义

发布于 2024-08-26 11:36:48 字数 93 浏览 4 评论 0原文

我试图弄清楚依赖属性到底是什么,但是当我在任何地方寻找定义时,我只找到“如何使用”,而不是“它是什么”。 想象一下,您在面试时被问到 - 什么是依赖属性。你的答案是什么?

I'm trying to figure out what exactly Dependency Properties are, but when I look anywhere for a definition, I only find "how to use" but not "what it is".
Imagine you are asked on a job interview - what is a dependency property. What would be your answer?

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

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

发布评论

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

评论(5

甜味超标? 2024-09-02 11:36:48

DependencyProperty 是一个属性,其值取决于(或可以取决于)某些其他源(例如动画、数据绑定、样式或可视化树继承)。常规属性的值存储在它所属的对象中,而您可以将依赖属性视为存储在数据库中的某个位置。该数据库本质上由一个字典组成,该字典将(对象,属性)对映射到其值,以及哪些属性依赖于其他属性的映射(例如,当您更改面板的 DataContext 时,它可以通知面板内的所有子级)。

那么为什么他们将属性值存储在某个神奇的数据库中呢?有几个原因:

  • 它减少了存储空间。向类添加属性(即使其值为 null)会为该类的每个实例添加 4 个字节(对于 64 位进程为 8 个字节)的空间。仅当实例具有值时,DependencyProperty 才会占用空间。例如,FrameworkElement 具有数十个依赖属性,其中大多数从未分配值。如果所有这些属性都存储在类中,则每个实例将有数百个字节。相反,每个实例只有大约 40 字节。

  • 它启用附加属性。 Canvas.LeftGrid.Row 等属性必须存储在从未听说过 CanvasGrid,那么你把它们放在哪里呢?您将它们放在数据库中的某个位置。

  • 它支持自动属性更改。想象一下您将如何实现样式或属性继承之类的东西(能够在父元素上设置字体或数据上下文之类的东西,并将其值传播到所有子元素)。将所有这些存储在数据库中使得代码全部集中在一个位置,而不是为需要它的每个对象和属性单独实现。

A DependencyProperty is a property whose value depends (or can depend) on some other source (such as animation, data binding, styles, or visual tree inheritance). A regular property's value is stored in the object it belongs to, while you can think of a dependency property as being stored in a database somewhere. This database is essentially composed of a dictionary that maps (object, property) pairs to their values, along with a mapping of which properties depend on other properties (e.g. so when you change the DataContext of a Panel, it can notify all the children inside the panel).

So why do they store property values in some magic database somewhere? There are a few reasons:

  • It reduces storage space. Adding a property (even if its value is null) to a class adds 4 bytes (8 for a 64-bit process) of space to every instance of the class. A DependencyProperty only takes up space when an instance has a value. For example, a FrameworkElement has dozens of dependency properties, most of which are never assigned values. If all those properties were stored in the class, each instance would be hundreds of bytes. Instead each instance is only about 40 bytes.

  • It enables attached properties. Properties like Canvas.Left and Grid.Row have to be stored on objects that have never heard of a Canvas or Grid, so where do you put them? You put them in a database somewhere.

  • It enables automatic property changes. Imagine how you would implement something like styles or property inheritance (the ability to set something like a font or data context on a parent element and have its value propagate to all child elements). Having all of this stored in a database makes it so the code is all in one place instead of being implemented separately for each object and property that needs it.

几度春秋 2024-09-02 11:36:48

“为您提供了一系列基础设施,可以完成您经常想要对普通属性执行的所有操作 - 验证它、将其强制到适当的范围、发出更改通知以及许多其他方面。”

WPF 教程 - 依赖属性简介

"gives you a bunch of infrastructure to do all the things that you often want to do with a normal property - validate it, coerce it into a proper range, give out change notifications, and a number of other aspects."

WPF Tutorial - Introduction To Dependency Properties

陪你到最终 2024-09-02 11:36:48

依赖属性是由 WPF 属性系统而不是声明类中的字段支持的属性。

这样做的意义在于,由于 WPF 拥有该属性,因此 WPF 在计算属性值时可以考虑各种因素,例如动画、样式和数据绑定。另一个结果是,由于属性由 WPF 管理,因此不必在概念上具有状态的类上声明它们:因此,附加属性允许网格将特定于网格的状态与非网格对象相关联。

(顺便说一句,我在上面提到了 WPF,因为这是使用 DP 的主要框架,但 Windows Workflow Foundation 也有依赖属性的概念。因此,严格正确地说,DP 是由外部属性支持的属性系统,特别是在获取属性值时允许“最后设定值”以外的因素发挥作用的系统。)

A dependency property is a property that is backed by the WPF property system instead of by a field in the declaring class.

The significance of this is that, because WPF owns the property, WPF can factor in various considerations when calculating the property value -- such as animations, styles and data bindings. Another consequence is that because properties are managed by WPF they don't have to be declared on the classes that conceptually have the state: hence, atttached properties, which allow e.g. a Grid to associate Grid-specific state with non-Grid objects.

(By the way, I've mentioned WPF above because this is the main framework that uses DPs, but Windows Workflow Foundation also has the notion of dependency properties. So to be strictly correct a DP is a property that is backed by an external property system, specifically one which allows factors other than "the last set value" to come into play when getting the property value.)

凯凯我们等你回来 2024-09-02 11:36:48

MSDN提供了很好的定义、描述和示例,

以便更深入地理解DependencyProperty 检查此处

MSDN provides a good definition, description and examples

For more deep understanding of DependencyProperty check here

提笔落墨 2024-09-02 11:36:48

依赖属性依赖于多个提供者来确定其在任何时间点的值。这些提供程序可以是不断更改其值的动画、属性值向下传播到其子元素的父元素,等等。

可以说,依赖属性的最大特征是其提供更改通知的内置功能。

每当依赖属性的值发生更改时,WPF 就可以自动触发许多操作,具体取决于属性的元数据。这些动作可以重新渲染 -
适当的元素,更新当前布局,刷新数据绑定,以及
更多。此内置更改通知启用的最有趣的功能之一是
化是属性触发器,它使您能够在以下情况下执行自己的自定义操作:
属性值改变,无需编写任何程序代码

A dependency property depends on multiple providers for determining its value at any point in time. These providers could be an animation continuously changing its value, a parent element whose property value propagates down to its children, and so on.

Arguably the biggest feature of a dependency property is its built-in ability to provide change notification.

Whenever the value of a dependency property changes, WPF can automatically trigger a number of actions, depending on the property’s metadata. These actions can be re-render-
ing the appropriate elements, updating the current layout, refreshing data bindings, and
much more. One of the most interesting features enabled by this built-in change notifica-
tion is property triggers, which enable you to perform your own custom actions when a
property value changes, without writing any procedural code

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