DependencyObject DependencyObject DependencyObject 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
type DependencyObject = class
    inherit DispatcherObject
Public Class DependencyObject
Inherits DispatcherObject
继承
DependencyObjectDependencyObjectDependencyObjectDependencyObject
派生
属性

示例

下面的示例从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
    {
        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

注解

类可对Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)其许多派生类启用属性系统服务。 DependencyObjectThe 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() DependencyObject() DependencyObject() DependencyObject()

初始化 DependencyObject 类的新实例。Initializes a new instance of the DependencyObject class.

属性

DependencyObjectType DependencyObjectType DependencyObjectType DependencyObjectType

获取对此实例的 CLRCLR 类型进行包装的 DependencyObjectTypeGets the DependencyObjectType that wraps the CLRCLR type of this instance.

Dispatcher Dispatcher Dispatcher Dispatcher

获取与此 Dispatcher 关联的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(Inherited from DispatcherObject)
IsSealed IsSealed IsSealed IsSealed

获取一个值,该值指示此实例当前是否为密封的(只读)。Gets a value that indicates whether this instance is currently sealed (read-only).

方法

CheckAccess() CheckAccess() CheckAccess() CheckAccess()

确定调用线程是否可以访问此 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)
ClearValue(DependencyProperty) ClearValue(DependencyProperty) ClearValue(DependencyProperty) ClearValue(DependencyProperty)

清除属性的本地值。Clears the local value of a property. 要清除的属性由 DependencyProperty 标识符指定。The property to be cleared is specified by a DependencyProperty identifier.

ClearValue(DependencyPropertyKey) ClearValue(DependencyPropertyKey) ClearValue(DependencyPropertyKey) ClearValue(DependencyPropertyKey)

清除只读属性的本地值。Clears the local value of a read-only property. 要清除的属性由 DependencyPropertyKey 指定。The property to be cleared is specified by a DependencyPropertyKey.

CoerceValue(DependencyProperty) CoerceValue(DependencyProperty) CoerceValue(DependencyProperty) CoerceValue(DependencyProperty)

对指定依赖属性的值进行强制。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.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定提供的 DependencyObject 是否等效于当前 DependencyObjectDetermines whether a provided DependencyObject is equivalent to the current DependencyObject.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

获取此 DependencyObject 的哈希代码。Gets a hash code for this DependencyObject.

GetLocalValueEnumerator() GetLocalValueEnumerator() GetLocalValueEnumerator() GetLocalValueEnumerator()

创建一个专用的枚举数,用于确定哪些依赖项属性在此 DependencyObject 上具有以本地方式设置的值。Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject.

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
GetValue(DependencyProperty) GetValue(DependencyProperty) GetValue(DependencyProperty) GetValue(DependencyProperty)

DependencyObject 的此实例返回依赖属性的当前有效值。Returns the current effective value of a dependency property on this instance of a DependencyObject.

InvalidateProperty(DependencyProperty) InvalidateProperty(DependencyProperty) InvalidateProperty(DependencyProperty) InvalidateProperty(DependencyProperty)

重新计算指定依赖项属性的有效值Re-evaluates the effective value for the specified dependency property

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
OnPropertyChanged(DependencyPropertyChangedEventArgs) OnPropertyChanged(DependencyPropertyChangedEventArgs) OnPropertyChanged(DependencyPropertyChangedEventArgs) OnPropertyChanged(DependencyPropertyChangedEventArgs)

每当更新此 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.

ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty)

如果存在,则返回依赖属性的本地值。Returns the local value of a dependency property, if it exists.

SetCurrentValue(DependencyProperty, Object) SetCurrentValue(DependencyProperty, Object) SetCurrentValue(DependencyProperty, Object) SetCurrentValue(DependencyProperty, Object)

设置依赖属性的值而不更改其值源。Sets the value of a dependency property without changing its value source.

SetValue(DependencyProperty, Object) SetValue(DependencyProperty, Object) SetValue(DependencyProperty, Object) SetValue(DependencyProperty, Object)

设置依赖属性的本地值,该值由其依赖属性标识符指定。Sets the local value of a dependency property, specified by its dependency property identifier.

SetValue(DependencyPropertyKey, Object) SetValue(DependencyPropertyKey, Object) SetValue(DependencyPropertyKey, Object) SetValue(DependencyPropertyKey, Object)

设置一个只读依赖属性的本地值,该值由依赖属性的 DependencyPropertyKey 标识符指定。Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.

ShouldSerializeProperty(DependencyProperty) ShouldSerializeProperty(DependencyProperty) ShouldSerializeProperty(DependencyProperty) ShouldSerializeProperty(DependencyProperty)

返回一个值,该值指示序列化进程是否应序列化所提供的依赖属性的值。Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
VerifyAccess() VerifyAccess() VerifyAccess() VerifyAccess()

强制调用线程具有此 DispatcherObject 的访问权限。Enforces that the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)

适用于

另请参阅