DependencyProperty.ReadOnly 属性

定义

获取一个值,该值指示由此 DependencyProperty 实例标识的依赖项对象是否为只读依赖项对象。Gets a value that indicates whether the dependency property identified by this DependencyProperty instance is a read-only dependency property.

public:
 property bool ReadOnly { bool get(); };
public bool ReadOnly { get; }
member this.ReadOnly : bool
Public ReadOnly Property ReadOnly As Boolean

属性值

如果该依赖项对象为只读属性,则为 true;否则为 falsetrue if the dependency property is read-only; otherwise, false.

示例

下面的示例从各种依赖属性字段获取默认的元数据和依赖属性标识符属性,并使用该信息来填充表以实现 "元数据浏览器"。The following example obtains the default metadata and the dependency property identifier properties from various dependency property fields, and uses the information to populate a table to implement a "metadata browser".

pm = dp.GetMetadata(dp.OwnerType);
MetadataClass.Text = pm.GetType().Name;
TypeofPropertyValue.Text = dp.PropertyType.Name;
DefaultPropertyValue.Text = (pm.DefaultValue!=null) ? pm.DefaultValue.ToString() : "null";
HasCoerceValue.Text = (pm.CoerceValueCallback == null) ? "No" : pm.CoerceValueCallback.Method.Name;
HasPropertyChanged.Text = (pm.PropertyChangedCallback == null) ? "No" : pm.PropertyChangedCallback.Method.Name;
ReadOnly.Text = (dp.ReadOnly) ? "Yes" : "No";
pm = dp.GetMetadata(dp.OwnerType)
MetadataClass.Text = pm.GetType().Name
TypeofPropertyValue.Text = dp.PropertyType.Name
DefaultPropertyValue.Text = If((pm.DefaultValue IsNot Nothing), pm.DefaultValue.ToString(), "null")
HasCoerceValue.Text = If((pm.CoerceValueCallback Is Nothing), "No", pm.CoerceValueCallback.Method.Name)
HasPropertyChanged.Text = If((pm.PropertyChangedCallback Is Nothing), "No", pm.PropertyChangedCallback.Method.Name)
    [ReadOnly].Text = If((dp.ReadOnly), "Yes", "No")

注解

只读依赖属性是通过调用 RegisterReadOnly 方法(而不是 Register 方法)在属性系统中注册的。Read-only dependency properties are registered within the property system by calling the RegisterReadOnly method as opposed to the Register method. 附加属性还可以注册为只读属性;请参阅 RegisterAttachedReadOnlyAttached properties can also be registered as read-only; see RegisterAttachedReadOnly.

只读依赖属性需要 DependencyPropertyKey 标识符而不是 DependencyProperty 标识符来执行元数据操作,例如重写元数据或设置值。Read-only dependency properties require a DependencyPropertyKey identifier rather than a DependencyProperty identifier to perform metadata operations such as overriding the metadata or setting the value. 如果通过调用 GetLocalValueEnumerator 或公开标识符的另一个 API 获取了 DependencyProperty 标识符的集合,请检查 ReadOnly 值,然后尝试使用该依赖项属性标识符作为输入参数调用 SetValueOverrideMetadata,用于验证标识符所表示的依赖项属性是否不是只读的。If you obtained a collection of DependencyProperty identifiers through a call to GetLocalValueEnumerator or another API that exposes identifiers, check the ReadOnly value before attempting to call SetValue or OverrideMetadata using that dependency property identifier as an input parameter, to verify that the dependency property that the identifier represents is not read-only. 如果 ReadOnly 的值 true 在依赖属性上,则无法通过编程方式从元数据或从 DependencyProperty 标识符获取对该依赖属性的 DependencyPropertyKey 标识符的引用;标识符必须可用作静态字段,才能对只读依赖项属性调用 SetValue(DependencyPropertyKey, Object)If the value of ReadOnly is true on a dependency property, there is no programmatic way to obtain a reference to the DependencyPropertyKey identifier of that dependency property, from the metadata or from the DependencyProperty identifier; the identifier must be available as a static field in order to call SetValue(DependencyPropertyKey, Object) against a read-only dependency property.

在创建自定义依赖属性并将其注册为只读时,应只为 CLRCLR 包装器属性定义 get 访问器。When you create a custom dependency property, and register it as read-only, you should define only a get accessor for the CLRCLR wrapper property. 否则,与支持依赖项属性的访问相比,你的类将具有属性包装的混乱的对象模型。Otherwise, your class will have a confusing object model for the property wrapper as compared to the access to the backing dependency property. 有关详细信息,请参阅自定义依赖属性只读依赖项属性For details, see Custom Dependency Properties or Read-Only Dependency Properties.

适用于

另请参阅