我可以创建一个只读的依赖属性吗?

时间:2022-06-04 06:30:13

I need this dependency property to be accessible for read operations only. Outside objects should not be able to assign values to it.

我需要这个依赖属性只对读取操作具有可访问性。外部对象不应该能够给它赋值。

2 个解决方案

#1


7  

Yes, of course (think IsMouseOver for why read-only dependency properties should exist). MSDN has a great article on the subject.

是的,当然(考虑一下为什么要有只读依赖属性)。MSDN有一篇关于这个主题的好文章。

There are some issues implementing read-only dependency properties and in some situations they will not work. However, it is possible in some cases. For those cases, the following is a brief guide to implementing a readonly dependency property:

实现只读依赖项属性有一些问题,在某些情况下它们不能工作。然而,这在某些情况下是可能的。对于这些情况,以下是实现只读依赖项属性的简要指南:

  1. Use RegisterReadOnly instead of Register.
  2. 使用RegisterReadOnly代替Register。
  3. Do not expose a public set method in your wrapped property.
  4. 不要在已包装的属性中公开公共集方法。
  5. The return value from RegisterReadOnly is of type DependencyPropertyKey (instead of the usual DependencyProperty). Store but do not expose this value.
  6. RegisterReadOnly的返回值是类型DependencyPropertyKey(而不是通常的DependencyProperty)。存储但不要公开此值。

The linked article will give you the necessary details.

链接的文章将提供必要的细节。

#2


4  

Yes, you can.

是的,你可以。

MSDN has an entire section describing how to define and use Read-Only Dependency Properties.

MSDN有一整节描述如何定义和使用只读依赖属性。

The main issue is to call DependencyProperty.RegisterReadOnly instead of Register. Also, if you create a property on the object to handle this, make sure to only implement a public getter in the property, and not a public setter.

主要问题是调用DependencyProperty。RegisterReadOnly代替注册。此外,如果您在对象上创建一个属性来处理此问题,请确保只在属性中实现公共getter,而不是公共setter。

#1


7  

Yes, of course (think IsMouseOver for why read-only dependency properties should exist). MSDN has a great article on the subject.

是的,当然(考虑一下为什么要有只读依赖属性)。MSDN有一篇关于这个主题的好文章。

There are some issues implementing read-only dependency properties and in some situations they will not work. However, it is possible in some cases. For those cases, the following is a brief guide to implementing a readonly dependency property:

实现只读依赖项属性有一些问题,在某些情况下它们不能工作。然而,这在某些情况下是可能的。对于这些情况,以下是实现只读依赖项属性的简要指南:

  1. Use RegisterReadOnly instead of Register.
  2. 使用RegisterReadOnly代替Register。
  3. Do not expose a public set method in your wrapped property.
  4. 不要在已包装的属性中公开公共集方法。
  5. The return value from RegisterReadOnly is of type DependencyPropertyKey (instead of the usual DependencyProperty). Store but do not expose this value.
  6. RegisterReadOnly的返回值是类型DependencyPropertyKey(而不是通常的DependencyProperty)。存储但不要公开此值。

The linked article will give you the necessary details.

链接的文章将提供必要的细节。

#2


4  

Yes, you can.

是的,你可以。

MSDN has an entire section describing how to define and use Read-Only Dependency Properties.

MSDN有一整节描述如何定义和使用只读依赖属性。

The main issue is to call DependencyProperty.RegisterReadOnly instead of Register. Also, if you create a property on the object to handle this, make sure to only implement a public getter in the property, and not a public setter.

主要问题是调用DependencyProperty。RegisterReadOnly代替注册。此外,如果您在对象上创建一个属性来处理此问题,请确保只在属性中实现公共getter,而不是公共setter。