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。有关详细信息,请参阅 Attached 属性概述。)然后,可以在派生自 DependencyObject 的任何类上设置附加属性。

  • 获取、设置和清除存在于任何依赖属性的值的 DependencyObject实用工具方法。

  • 元数据、强制值支持、属性更改通知和重写依赖项属性或附加属性的回调。 此外,该 DependencyObject 类还有助于依赖属性的按所有者属性元数据。

  • 派生自 ContentElementFreezableVisual的类的公用基类。 (UIElement,另一个基元素类,具有包含 Visual.)

构造函数

名称 说明
DependencyObject()

初始化 DependencyObject 类的新实例。

属性

名称 说明
DependencyObjectType

获取包装 DependencyObjectType 此实例的 CLR 类型。

Dispatcher

获取与此DispatcherDispatcherObject关联的值。

(继承自 DispatcherObject)
IsSealed

获取一个值,该值指示此实例当前是否密封(只读)。

方法

名称 说明
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)

适用于

另请参阅