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 也有助於相依性屬性的個別擁有者屬性中繼資料。

  • 衍生自 ContentElementFreezableVisual 之類別的通用基類。 UIElement (,另一個基底元素類別具有類別階層,其中包含 Visual .)

建構函式

DependencyObject()

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

屬性

DependencyObjectType

取得包裝 DependencyObjectType 這個實例 CLR 型別的 。

Dispatcher

取得與這個 Dispatcher 關聯的 DispatcherObject

(繼承來源 DispatcherObject)
IsSealed

取得值,這個值表示此執行個體目前是否已密封 (唯讀)。

方法

CheckAccess()

判斷呼叫的執行是否可以存取這個 DispatcherObject

(繼承來源 DispatcherObject)
ClearValue(DependencyProperty)

清除屬性的區域數值。 要清除的屬性是由 DependencyProperty 識別項所指定。

ClearValue(DependencyPropertyKey)

清除唯讀屬性的區域數值。 要清除的屬性是由 DependencyPropertyKey 所指定。

CoerceValue(DependencyProperty)

強制轉型所指定相依性屬性的值。 完成方式是叫用存在於呼叫 DependencyObject 之相依性屬性的屬性中繼資料中所指定的任何 CoerceValueCallback 函式。

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)

適用於

另請參閱