DependencyObject 類別

定義

代表參與依賴屬性系統的物件。

public ref class DependencyObject : System::Windows::Threading::DispatcherObject
public class DependencyObject : System.Windows.Threading.DispatcherObject
[System.Windows.Markup.NameScopeProperty("NameScope", typeof(System.Windows.NameScope))]
public class DependencyObject : System.Windows.Threading.DispatcherObject
type DependencyObject = class
    inherit DispatcherObject
[<System.Windows.Markup.NameScopeProperty("NameScope", typeof(System.Windows.NameScope))>]
type DependencyObject = class
    inherit DispatcherObject
Public Class DependencyObject
Inherits DispatcherObject
繼承
DependencyObject
衍生
屬性

範例

以下範例由 DependencyObject 創建一個新的抽象類別衍生而來。 該類別接著註冊一個附加屬性,並包含該附加屬性的支援成員。

public abstract class AquariumObject3 : DependencyObject
{
    public enum Bouyancy
    {
        Floats,
        Sinks,
        Drifts
    }
    public static readonly DependencyProperty BouyancyProperty = DependencyProperty.RegisterAttached(
      "Bouyancy",
      typeof(Bouyancy),
      typeof(AquariumObject3),
      new FrameworkPropertyMetadata(Bouyancy.Floats, FrameworkPropertyMetadataOptions.AffectsArrange),
      new ValidateValueCallback(ValidateBouyancy)
    );
    public static void SetBouyancy(UIElement element, Bouyancy value)
    {
        element.SetValue(BouyancyProperty, value);
    }
    public static Bouyancy GetBouyancy(UIElement element)
    {
        return (Bouyancy)element.GetValue(BouyancyProperty);
    }
    private static bool ValidateBouyancy(object value)
    {
        Bouyancy bTest = (Bouyancy) value;
        return (bTest == Bouyancy.Floats || bTest == Bouyancy.Drifts || bTest==Bouyancy.Sinks);
    }
    public static readonly DependencyProperty IsDirtyProperty = DependencyProperty.Register(
      "IsDirty",
      typeof(Boolean),
      typeof(AquariumObject3)
    );
}
Public MustInherit Class AquariumObject3
    Inherits DependencyObject
    Public Enum Bouyancy
        Floats
        Sinks
        Drifts
    End Enum
    Public Shared ReadOnly BouyancyProperty As DependencyProperty = DependencyProperty.RegisterAttached("Bouyancy", GetType(Bouyancy), GetType(AquariumObject3), New FrameworkPropertyMetadata(Bouyancy.Floats, FrameworkPropertyMetadataOptions.AffectsArrange), New ValidateValueCallback(AddressOf ValidateBouyancy))
    Public Shared Sub SetBouyancy(ByVal element As UIElement, ByVal value As Bouyancy)
        element.SetValue(BouyancyProperty, value)
    End Sub
    Public Shared Function GetBouyancy(ByVal element As UIElement) As Bouyancy
        Return CType(element.GetValue(BouyancyProperty), Bouyancy)
    End Function
    Private Shared Function ValidateBouyancy(ByVal value As Object) As Boolean
        Dim bTest As Bouyancy = CType(value, Bouyancy)
        Return (bTest = Bouyancy.Floats OrElse bTest = Bouyancy.Drifts OrElse bTest = Bouyancy.Sinks)
    End Function
    Public Shared ReadOnly IsDirtyProperty As DependencyProperty = DependencyProperty.Register("IsDirty", GetType(Boolean), GetType(AquariumObject3))
End Class

備註

DependencyObject 類別使其衍生類別Windows Presentation Foundation(WPF)屬性系統服務成為可能。

屬性系統的主要功能是計算屬性的值,並提供系統通知變更的值。 另一個參與屬性系統的關鍵類別是 DependencyPropertyDependencyProperty 使得依賴屬性能註冊到屬性系統,並提供每個依賴屬性的識別與資訊,而基底類別則 DependencyObject 允許物件使用依賴屬性。

DependencyObject 服務與特性包括以下幾項:

  • 依賴性物業託管支援。 你透過呼叫 Register 該方法,並將方法的回傳值存為類別中的公開靜態欄位來註冊相依屬性。

  • 附帶的物業託管支援。 你透過呼叫 RegisterAttached 該方法註冊附加屬性,並將方法的回傳值存為類別中的公開靜態唯讀欄位。 (還有額外的成員要求;請注意,這是針對附加屬性的 WPF 專屬實作。詳情請參見附加屬性概覽.)你所附的屬性可以設定在任何源自 DependencyObject 的類別上。

  • 取得、設定並清除存在於 DependencyObject的依賴性質值的效用方法。

  • 元資料、強制值支援、屬性變更通知,以及覆寫相依屬性或附加屬性的回調。 此外,該 DependencyObject 類別也促進了依賴屬性的每個擁有者屬性元資料。

  • 一個 ContentElement通用的基底類別,用於由 、 Freezable、 或 Visual衍生的類別。 (UIElement另一個基底元素類別,具有包含的 Visual類別階層。)

建構函式

名稱 Description
DependencyObject()

初始化 DependencyObject 類別的新執行個體。

屬性

名稱 Description
DependencyObjectType

會取得 DependencyObjectType 包裹此實例 CLR 類型的 。

Dispatcher

了解 Dispatcher 這與此 DispatcherObject 有關。

(繼承來源 DispatcherObject)
IsSealed

會獲得一個值,表示該實例目前是否封存(唯讀)。

方法

名稱 Description
CheckAccess()

判斷呼叫執行緒是否能存取此 DispatcherObject

(繼承來源 DispatcherObject)
ClearValue(DependencyProperty)

清算房產的當地價值。 要清除的屬性由識別 DependencyProperty 碼指定。

ClearValue(DependencyPropertyKey)

清除只讀屬性的局部值。 要清除的屬性由 DependencyPropertyKey指定。

CoerceValue(DependencyProperty)

強制設定指定的依賴性質值。 這是透過在屬性CoerceValueCallback中指定的依賴屬性元資料中,呼叫 的依賴屬性中的任何DependencyObject函式來達成的。

Equals(Object)

判斷所給的 DependencyObject 是否等同於電流 DependencyObject

GetHashCode()

會得到一個 DependencyObject雜湊碼。

GetLocalValueEnumerator()

建立專門的枚舉器,用以判斷哪些相依屬性在局部 DependencyObject設定值。

GetType()

取得目前實例的 Type

(繼承來源 Object)
GetValue(DependencyProperty)

回傳此實例 DependencyObject上依賴屬性的當前有效值。

InvalidateProperty(DependencyProperty)

重新評估指定相依性質的有效值。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
OnPropertyChanged(DependencyPropertyChangedEventArgs)

當任何依賴性財產 DependencyObject 的實際價值更新時,該權利會被調用。 變更的具體相依屬性會在事件資料中報告。

ReadLocalValue(DependencyProperty)

回傳依賴屬性的局部值(若存在)。

SetCurrentValue(DependencyProperty, Object)

設定依賴屬性的值,且不改變其值來源。

SetValue(DependencyProperty, Object)

設定依賴屬性的局部值,並由其依賴屬性識別碼指定。

SetValue(DependencyPropertyKey, Object)

設定只讀相依屬性的本地值,該值由 DependencyPropertyKey 相依屬性的識別碼指定。

ShouldSerializeProperty(DependencyProperty)

回傳一個值,指示序列化程序是否應該將所提供的相依屬性的值序列化。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
VerifyAccess()

強制呼叫執行緒能存取此 DispatcherObject

(繼承來源 DispatcherObject)

適用於

另請參閱