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 bool ReadOnly { get; }

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

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

Producto Versiones
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also