如何创建只读依赖属性?
如何创建只读依赖属性? 这样做的最佳实践是什么?
具体来说,最让我困惑的是,没有任何实现
DependencyObject.GetValue()
将 System.Windows.DependencyPropertyKey 作为参数。
System.Windows.DependencyProperty.RegisterReadOnly
返回 DependencyPropertyKey
对象,而不是 DependencyProperty
。 那么,如果无法调用 GetValue,该如何访问只读依赖属性呢? 或者您是否应该以某种方式将 DependencyPropertyKey 转换为普通的旧 DependencyProperty 对象?
建议和/或代码将不胜感激!
How do you create a read-only dependancy property? What are the best-practices for doing so?
Specifically, what's stumping me the most is the fact that there's no implementation of
DependencyObject.GetValue()
that takes a System.Windows.DependencyPropertyKey
as a parameter.
System.Windows.DependencyProperty.RegisterReadOnly
returns a DependencyPropertyKey
object rather than a DependencyProperty
. So how are you supposed to access your read-only dependency property if you can't make any calls to GetValue? Or are you supposed to somehow convert the DependencyPropertyKey
into a plain old DependencyProperty
object?
Advice and/or code would be GREATLY appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,这很简单(通过 RegisterReadOnly):
仅当您在私有/受保护/内部代码中设置值时才使用该密钥。 由于受保护的
ReadOnlyProp
setter,这对您来说是透明的。It's easy, actually (via RegisterReadOnly):
You use the key only when you set the value in private/protected/internal code. Due to the protected
ReadOnlyProp
setter, this is transparent to you.我想指出的是,目前最好使用 https://github.com/HavenDV/DependencyPropertyGenerator< /a>,代码将非常简单:
I want to note that at the current time it is better to use https://github.com/HavenDV/DependencyPropertyGenerator, the code will be extremely simple: