DependencyObject Class


代表參與相依性屬性系統的物件。Represents an object that participates in the dependency property system.

public ref class DependencyObject : System::Windows::Threading::DispatcherObject
[System.Windows.Markup.NameScopeProperty("NameScope", typeof(System.Windows.NameScope))]
public class DependencyObject : System.Windows.Threading.DispatcherObject
public class DependencyObject : System.Windows.Threading.DispatcherObject
type DependencyObject = class
    inherit DispatcherObject
Public Class DependencyObject
Inherits DispatcherObject


下列範例衍生自 DependencyObject 以建立新的抽象類別。The following example derives from DependencyObject to create a new abstract class. 類別接著會註冊附加屬性,並包含該附加屬性的支援成員。The class then registers an attached property and includes support members for that attached property.

public abstract class AquariumObject3 : DependencyObject
    public enum Bouyancy
    public static readonly DependencyProperty BouyancyProperty = DependencyProperty.RegisterAttached(
      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(
Public MustInherit Class AquariumObject3
    Inherits DependencyObject
    Public Enum Bouyancy
    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)Windows Presentation Foundation (WPF) 屬性系統服務。The DependencyObject class enables Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) property system services on its many derived classes.

屬性系統的主要功能是要計算屬性的值,並提供已變更值的系統通知。The property system's primary function is to compute the values of properties, and to provide system notification about values that have changed. 參與屬性系統的另一個主要類別是 DependencyPropertyAnother key class that participates in the property system is DependencyProperty. DependencyProperty 可將相依性屬性註冊到屬性系統中,並提供每個相依性屬性的識別和資訊,而 DependencyObject 做為基類可讓物件使用相依性屬性。DependencyProperty enables the registration of dependency properties into the property system, and provides identification and information about each dependency property, whereas DependencyObject as a base class enables objects to use the dependency properties.

DependencyObject 的服務和特性包括下列各項:DependencyObject services and characteristics include the following:

  • 相依性屬性裝載支援。Dependency property hosting support. 您可以藉由呼叫 Register 方法來註冊相依性屬性,並將方法的傳回值儲存為類別中的公用靜態欄位。You register a dependency property by calling the Register method, and storing the method's return value as a public static field in your class.

  • 附加屬性裝載支援。Attached property hosting support. 您可以藉由呼叫 RegisterAttached 方法來註冊附加屬性,並將方法的傳回值儲存為類別中的公用靜態唯讀欄位。You register an attached property by calling the RegisterAttached method, and storing the method's return value as a public static read-only field in your class. (也有其他的成員需求,請注意,這代表附加屬性的 WPFWPF 特定的實值。(There are also additional member requirements; note that this represents a WPFWPF specific implementation for attached properties. 如需詳細資訊,請參閱附加屬性總覽。)然後您可以在任何衍生自 DependencyObject的類別上設定附加屬性。For details, see Attached Properties Overview.) Your attached property can then be set on any class that derives from DependencyObject.

  • 取得、設定和清除存在於 DependencyObject上任何相依性屬性值的公用程式方法。Get, set, and clear utility methods for values of any dependency properties that exist on the DependencyObject.

  • 中繼資料、強制型轉值支援、屬性變更的通知,以及覆寫相依性屬性或附加屬性的回呼。Metadata, coerce value support, property changed notification, and override callbacks for dependency properties or attached properties. 此外,DependencyObject 類別可協助相依性屬性的每個擁有者屬性中繼資料。Also, the DependencyObject class facilitates the per-owner property metadata for a dependency property.

  • 衍生自 ContentElementFreezableVisual之類別的通用基類。A common base class for classes derived from ContentElement, Freezable, or Visual. UIElement,另一個基底元素類別具有包含 Visual的類別階層)。(UIElement, another base element class, has a class hierarchy that includes Visual.)



DependencyObject 類別的新執行個體初始化。Initializes a new instance of the DependencyObject class.



取得包裝此執行個體之 CLRCLR 型別的 DependencyObjectTypeGets the DependencyObjectType that wraps the CLRCLR type of this instance.


取得與這個 Dispatcher 關聯的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(Inherited from DispatcherObject)

取得值,這個值表示此執行個體目前是否已密封 (唯讀)。Gets a value that indicates whether this instance is currently sealed (read-only).



判斷呼叫的執行是否可以存取這個 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)

清除屬性的區域數值。Clears the local value of a property. 要清除的屬性是由 DependencyProperty 識別項所指定。The property to be cleared is specified by a DependencyProperty identifier.


清除唯讀屬性的區域數值。Clears the local value of a read-only property. 要清除的屬性是由 DependencyPropertyKey 所指定。The property to be cleared is specified by a DependencyPropertyKey.


強制轉型所指定相依性屬性的值。Coerces the value of the specified dependency property. 完成方式是叫用存在於呼叫 DependencyObject 之相依性屬性的屬性中繼資料中所指定的任何 CoerceValueCallback 函式。This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject.


判斷提供的 DependencyObject 和目前的 DependencyObject 是否相等。Determines whether a provided DependencyObject is equivalent to the current DependencyObject.


取得這個 DependencyObject 的雜湊碼。Gets a hash code for this DependencyObject.


建立特定的列舉值,以判斷哪些相依性屬性在此 DependencyObject 上具有本機設定的值。Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject.


取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)

傳回 DependencyObject 的這個執行個體上之相依性屬性的目前有效值。Returns the current effective value of a dependency property on this instance of a DependencyObject.


重新評估指定相依性屬性的有效值。Re-evaluates the effective value for the specified dependency property.


建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)

每當這個 DependencyObject 上任何相依性屬性的有效值已更新時叫用。Invoked whenever the effective value of any dependency property on this DependencyObject has been updated. 已變更的特定相依性屬性會在事件資料中報告。The specific dependency property that changed is reported in the event data.


傳回相依性屬性的區域值 (如果存在)。Returns the local value of a dependency property, if it exists.

SetCurrentValue(DependencyProperty, Object)

設定相依性屬性的值,而不需要變更其值來源。Sets the value of a dependency property without changing its value source.

SetValue(DependencyProperty, Object)

設定相依性屬性的區域值 (由相依性屬性的識別碼所指定)。Sets the local value of a dependency property, specified by its dependency property identifier.

SetValue(DependencyPropertyKey, Object)

設定唯讀相依性屬性的區域數值 (由相依性屬性的 DependencyPropertyKey 識別項所指定)。Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.


傳回值,這個值表示序列化程序是否應該序列化所提供相依性屬性的值。Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property.


傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。Enforces that the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)

Applies to

See also