WPF:有什么区别从属属性和常规CLR属性? [英] WPF: What distinguishes a Dependency Property from a regular CLR Property?

查看:62
本文介绍了WPF:有什么区别从属属性和常规CLR属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,真正成为依赖属性意味着什么?

In WPF, what, really, does it mean to be a "dependency property"?

我阅读了Microsoft的依赖项属性概述,但对我而言并没有真正陷入困境。该文章的部分内容为:

I read Microsoft's Dependency Properties Overview, but it's not really sinking in for me. In part that article says:


样式和模板是使用依赖项属性的两种主要激励方案。样式对于设置定义应用程序用户界面(UI)的属性特别有用。样式通常在XAML中定义为资源。样式与属性系统进行交互是因为样式通常包含特定属性的设置器,以及基于另一个属性的实时值更改属性值的触发器。

Styles and templates are two of the chief motivating scenarios for using dependency properties. Styles are particularly useful for setting properties that define application user interface (UI). Styles are typically defined as resources in XAML. Styles interact with the property system because they typically contain "setters" for particular properties, as well as "triggers" that change a property value based on the real-time value for another property.

然后示例代码是:

<Style x:Key="GreenButtonStyle">
  <Setter Property="Control.Background" Value="Green"/>
</Style>
....
<Button Style="{StaticResource GreenButtonStyle}">I am green!</Button>

但是我没有得到什么特别的东西。这是否意味着我在按钮上将 Style 设置为给定样式时,实际上是在设置 Background 暗中?这是症结所在吗?

But I'm not getting what is special about this. Does it just imply, that when I set Style on the button to the given style, that I am actually setting Background implicitly? Is that the crux of it?

推荐答案


在WPF中,实际上意味着依赖属性是什么意思?

In WPF, what, really, does it mean to be a "dependency property"?

要成为依赖项属性,必须将该属性定义为 DependencyProperty 静态地放在类上。依赖项属性系统与标准CLR属性有很大不同。

In order to be a dependency property, the property must actually be defined as a DependencyProperty, statically, on the class. The dependency property system is very different than a standard CLR property.

依赖项属性的处理方式非常不同。类型定义静态的依赖项属性,并提供默认值。运行时实际上直到需要时才为实例生成值。这提供了一个好处-该属性在请求类型之前是不存在的,因此您可以拥有许多没有开销的属性。

Dependency properties are handled very differently, though. A type defines a dependency property statically, and provides a default value. The runtime actually doesn't generate a value for an instance until it's needed. This provides one benefit - the property doesn't exist until requested for a type, so you can have a large number of properties without overhead.

这就是样式的原因工作属性,但对于允许附加属性,通过可视树的属性继承以及WPF依赖的许多其他功能也很重要。

This is what makes the styling work property, but is also important to allow attached properties, property "inheritance" through the visual tree, and many other things WPF relies on.

例如,使用 DataContext 依赖项属性。通常,您为Window或UserControl设置 DataContext 依赖项属性。默认情况下,该窗口中的所有控件都会自动继承其父级的 DataContext 属性,这使您可以为控件指定数据绑定。使用标准的CLR属性,您需要为窗口中的每个控件定义DataContext,以使绑定正常工作。

For example, take the DataContext dependency property. Typically, you set the DataContext dependency property for a Window or a UserControl. All of the controls within that Window, by default, "inherit" their parent's DataContext proeprty automatically, which allows you to specify data bindings for controls. With a standard CLR property, you'd need to define that DataContext for every control in the window, just to get binding to work properly.

这篇关于WPF:有什么区别从属属性和常规CLR属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆