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

屬性值

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 方法(而非方法)在屬性系統中註冊 RegisterRead-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. 如果您 DependencyProperty 透過呼叫 GetLocalValueEnumerator 或公開識別碼的另一個 API 取得識別碼的集合,請 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 屬性上,則不會以程式設計方式 DependencyPropertyKey 從中繼資料或識別碼取得該相依性屬性的識別碼參考 DependencyProperty ; 識別碼必須可作為靜態欄位使用,才能針對唯讀相依性屬性進行呼叫 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.

當您建立自訂相依性屬性,並將它註冊為唯讀時,您應該只針對 CLR 包裝函式屬性定義 get 存取子。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.

適用於

另請參閱