DependencyPropertyKey.DependencyProperty 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取与此专用只读依赖项对象标识符关联的依赖项对象标识符。
public:
property System::Windows::DependencyProperty ^ DependencyProperty { System::Windows::DependencyProperty ^ get(); };
public System.Windows.DependencyProperty DependencyProperty { get; }
member this.DependencyProperty : System.Windows.DependencyProperty
Public ReadOnly Property DependencyProperty As DependencyProperty
属性值
相关的依赖项对象标识符。
示例
以下示例调用 DependencyProperty 公开 DependencyProperty 类上只读依赖属性的标识符 (AquariumGraphicProperty
) AquariumGraphic
。 该示例还显示创建DependencyPropertyKey (作为内部成员) 和 get 访问器。AquariumGraphic
internal static readonly DependencyPropertyKey AquariumSizeKey = DependencyProperty.RegisterReadOnly(
"AquariumSize",
typeof(double),
typeof(Aquarium),
new PropertyMetadata(double.NaN)
);
public static readonly DependencyProperty AquariumSizeProperty =
AquariumSizeKey.DependencyProperty;
public double AquariumSize
{
get { return (double)GetValue(AquariumSizeProperty); }
}
Friend Shared ReadOnly AquariumSizeKey As DependencyPropertyKey = DependencyProperty.RegisterReadOnly("AquariumSize", GetType(Double), GetType(Aquarium), New PropertyMetadata(Double.NaN))
Public Shared ReadOnly AquariumSizeProperty As DependencyProperty = AquariumSizeKey.DependencyProperty
Public ReadOnly Property AquariumSize() As Double
Get
Return CDbl(GetValue(AquariumSizeProperty))
End Get
End Property
注解
该值 DependencyProperty 使只读属性的标识符能够使用一些用于读写依赖属性的相同接口参与通用属性系统操作。
若要为只读依赖属性实现 get 属性访问器,应在类上创建和公开 DependencyProperty 标识符。 这有两种用途:
你自己的类需要 DependencyProperty 标识符才能为属性包装器实现 get 访问器。 将 DependencyProperty 用作实现 get 访问器的调用的参数 GetValue 。
DependencyProperty 标识符将依赖属性公开给属性系统,以便依赖元数据的其他方法可以通过标准形式访问它。 例如,如果 GetLocalValueEnumerator 对某些 DependencyObject 属性调用并获取了本地集属性的枚举, (值和标识符) 为只读依赖属性返回的标识符将是你的 DependencyProperty 值而不是键。 不公开 DependencyProperty 标识符不会以任何方式增加只读依赖属性的安全性,它只是使涉及属性的操作对后续派生类和类实例更加尴尬。
若要公开 DependencyProperty 类上的标识符,请直接调用 DependencyProperty 密钥。 使用此值在类上创建一个public static readonly
DependencyProperty与该类并行的DependencyPropertyKey标识符。