Gif*_*guy 63 .net wpf dependency-properties
如何创建只读依赖属性?这样做的最佳做法是什么?
具体来说,最让我感到困惑的是没有实施的事实
DependencyObject.GetValue()
Run Code Online (Sandbox Code Playgroud)
以a System.Windows.DependencyPropertyKey
为参数.
System.Windows.DependencyProperty.RegisterReadOnly
返回一个D ependencyPropertyKey
对象而不是一个DependencyProperty
.那么如果你不能对GetValue进行任何调用,你应该怎么访问你的只读依赖属性?或者你应该以某种方式将其DependencyPropertyKey
转换为普通的旧DependencyProperty
对象?
建议和/或代码将非常感谢!
Ken*_* K. 137
实际上很简单(通过RegisterReadOnly):
public class OwnerClass : DependencyObject // or DependencyObject inheritor
{
private static readonly DependencyPropertyKey ReadOnlyPropPropertyKey
= DependencyProperty.RegisterReadOnly("ReadOnlyProp", typeof(int), typeof(OwnerClass),
new FrameworkPropertyMetadata(default(int),
FrameworkPropertyMetadataOptions.None));
public static readonly DependencyProperty ReadOnlyPropProperty
= ReadOnlyPropPropertyKey.DependencyProperty;
public int ReadOnlyProp
{
get { return (int)GetValue(ReadOnlyPropProperty); }
protected set { SetValue(ReadOnlyPropPropertyKey, value); }
}
//your other code here ...
}
Run Code Online (Sandbox Code Playgroud)
只有在private/protected/internal代码中设置值时才使用密钥.由于受保护的ReadOnlyProp
二传手,这对您来说是透明的.
归档时间: |
|
查看次数: |
24286 次 |
最近记录: |