DependencyProperty.ReadOnly 屬性

定義

取得值,這個值表示此 DependencyProperty 執行個體所識別的相依性屬性是否為唯讀相依性屬性。

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

屬性值

Boolean

如果相依性屬性為唯讀則為 true,否則為 false

範例

下列範例會從各種相依性屬性欄位取得預設中繼資料和相依性屬性識別碼屬性,並使用資訊來填入資料表來實作「中繼資料瀏覽器」。

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 ,在屬性系統中註冊。 附加屬性也可以註冊為唯讀;請參閱 RegisterAttachedReadOnly

唯讀相依性屬性需要 DependencyPropertyKey 識別碼,而不是 DependencyProperty 識別碼來執行中繼資料作業,例如覆寫中繼資料或設定值。 如果您透過呼叫 GetLocalValueEnumerator 或其他公開識別碼的 DependencyProperty API 取得識別碼集合,請在嘗試呼叫 SetValue 或使用 OverrideMetadata 該相依性屬性識別碼做為輸入參數之前檢查 ReadOnly 值,以確認識別碼所代表的相依性屬性不是唯讀的。 如果 的值 ReadOnly 位於相依性屬性上,則沒有任何程式設計方式可從中繼資料或 DependencyProperty 識別碼取得該相依性屬性識別碼的參考 DependencyPropertyKey ;識別碼必須以靜態欄位的形式提供,才能針對唯讀相依性屬性呼叫 SetValue(DependencyPropertyKey, Object) true

當您建立自訂相依性屬性,並將其註冊為唯讀時,您應該只定義 CLR 包裝函式屬性的 get 存取子。 否則,相較于支援相依性屬性的存取,您的類別會有屬性包裝函式的混淆物件模型。 如需詳細資訊,請參閱 自訂相依性屬性唯讀相依性屬性

適用於

另請參閱