DependencyProperty.ReadOnly Property

Gets a value that indicates whether the dependency property identified by this DependencyProperty instance is a read-only dependency property.

Namespace:  System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)

public bool ReadOnly { get; }

Property Value

Type: System.Boolean
true if the dependency property is read-only; otherwise, false.

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.

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


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft