Bewerken

Share via


DependencyProperty.ReadOnly Property

Definition

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

Property Value

true if the dependency property is read-only; otherwise, false.

Examples

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")

Remarks

Read-only dependency properties are registered within the property system by calling the RegisterReadOnly method as opposed to the Register method. Attached properties can also be registered as read-only; see RegisterAttachedReadOnly.

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. 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. 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.

When you create a custom dependency property, and register it as read-only, you should define only a get accessor for the CLR 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.

Applies to

See also