我有一个关于 WPF 依赖属性的问题。注册依赖属性时,它被注册为 public static readonly。
- 如果是只读的,那么如何改变它的值呢?
- 如果是静态的,则只有一个实例。假设我有一个类按钮的依赖属性 someproperty。如果我有两个具有不同属性值的按钮。 WPF 是如何处理的?
我有点困惑。预先感谢您的帮助。
I have a question about WPF dependencyproperty. When the dependencyproperty is registered, it is registered as public static readonly.
- If it is readonly, how can the value be changed?
- If it is static, then there is only one instance. Let's say I have a dependencyproperty someproperty for class button. If I have two buttons that have different values of someproperty. How does WPF handle it?
I am a little confused. Thank you in advance for your help.
发布评论
评论(4)
DependencyProperty 的变量被标记为只读;在 .NET 中,这意味着只有变量是只读的。 变量引用的对象的状态仍然可以更改。有关
readonly
工作原理的详细信息,请参阅这篇 MSDN 文章.设置 DependencyProperty 时,需要两件事:属性的值,以及要设置该属性的所属类型的实例。 DependencyObject/DependencyProperty 系统管理哪些实例分配有哪些值。
DependencyProperty 对于类来说是静态的,因此只有一个 DependencyProperty 实例负责管理所有实例的 DP 状态。至于为什么会这样,需要对 DependencyObjects 进行大量解释。
The variable of the DependencyProperty is marked
readonly
; in .NET that means only the variable is read only. The state of the object referenced by the variable can still be changed. For more info about howreadonly
works, see this MSDN article.When a DependencyProperty is set, it requires two things: The value of the property, and the instance of the owning type that the property is to be set on. The DependencyObject/DependencyProperty system manages which instances have what values assigned to them.
The DependencyProperty is static to the class so that there is one single DependencyProperty instance responsible for managing the state of a DP for all instances. As to why this is requires a lot of explanation about DependencyObjects.
当您更改
DependencyProperty
的值时,你正在做的就是:改变它的值。您没有更改对DependencyProperty
实例的引用。因此,将其标记为只读是有效的。它类似于拥有某个类的实例。您可以将该实例标记为只读,同时仍然能够修改类的内部值。即:至于静态实例问题,我相信它与 DependencyProperty 值的设置方式有关 - 它们是根据属性的目标设置的,而不是属性本身(我思考)。我不完全确定这部分是如何工作的(我需要阅读它),但请查看 MSDN 有关依赖属性的概述,这可能提供了一些线索。
When you change the value of the
DependencyProperty
, you are doing just that: changing it's value. You are not changing the reference to theDependencyProperty
instance. Therefore, it is valid to have it marked asreadonly
. It is similar to having an instance of some class. You can mark that instancereadonly
while being still able to modify the internal values of the class. i.e.:As for the static instance question, I believe it has something to do with the way
DependencyProperty
values are set - they are set according to the target of the property, as opposed to the property itself (I think). I'm not entirely sure how this part works (I need to read up on it), but take a look over on MSDN for an overview on dependency properties, which may hold a clue to this.从
Register(...)
返回的对象是DependencyProperty
的描述符。那个是只读的;如果您愿意,您无法修改属性的属性 - 属性的元数据。 (例如,您无法修改继承行为。)但是,您可以使用
SetValue(...)
方法。为了使用 XAML 中的属性,您应该为DependencyProperty
声明一个包装器 CLR 属性,如链接文章中的示例所示。The object you receive back from
Register(...)
is the descriptor of theDependencyProperty
. That one is read-only; you cannot modify the properties of the property - the metadata of the property, if you like. (For example, you can't modify the inheritance behaviour.)You can, however, modify the value of the property, using the
SetValue(...)
method. And in order to use your property from XAML, you should declare a wrapper CLR property for yourDependencyProperty
, as in the example in the linked article.DependencyProperties 的工作方式类似于
Dictionary
。您始终设置对象的值,而不是 DependencyProperty 本身的值。这就是为什么它可以是静态的。它也是静态的,因为这样速度更快。DependencyProperties work similar to a
Dictionary<object,object>
. You always set the value for an object, and not a value for the DependencyProperty itself. That's why it can be static. It's also static because it's faster this way.