更新:2007 年 11 月
命名空间:
System.Windows 程序集:
WindowsBase(在 WindowsBase.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/xaml/presentation
Public Class DependencyObject _
Inherits DispatcherObject
Dim instance As DependencyObject
public class DependencyObject : DispatcherObject
public ref class DependencyObject : public DispatcherObject
public class DependencyObject extends DispatcherObject
public class DependencyObject extends DispatcherObject
DependencyObject 类对它的许多派生类启用 Windows Presentation Foundation (WPF) 属性系统服务。
属性系统的主要功能是计算属性的值,并提供有关值已更改的系统通知。参与属性系统的另一个关键类是 DependencyProperty。DependencyProperty 可实现在属性系统中注册依赖项属性,并提供有关每个依赖项属性的标识和信息,而 DependencyObject 作为一个基类则使对象能够使用依赖项属性。
DependencyObject 包括以下服务和特征:
依赖项属性宿主支持。通过调用 Register 方法并将该方法的返回值作为公共静态字段存储在类中,即可注册依赖项属性。
附加属性宿主支持。通过调用 RegisterAttached 方法并将该方法的返回值作为公共静态只读字段存储在类中,即可注册附加属性。(还有一些附加成员要求;请注意,这代表附加属性的 WPF 特定实现。有关详细信息,请参见附加属性概述。) 然后,可以在从 DependencyObject 派生的任何类上设置附加属性。
存在于 DependencyObject 上的任何依赖项属性值的 get、set 和 clear 实用工具方法。
依赖项属性或附加属性的元数据、强制值支持、属性更改通知以及重写回调。同时,DependencyObject 类简化了依赖项属性的每所有者属性元数据。
派生自 ContentElement、Freezable 或 Visual 的类的公共基类。(另一个基元素类 UIElement 具有一个包括 Visual 的类层次结构。)
下面的示例派生自 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)
);
}
System..::.Object
System.Windows.Threading..::.DispatcherObject
System.Windows..::.DependencyObject
System.Windows..::.ContentElement
System.Windows.Controls..::.GridViewColumn
System.Windows.Controls..::.TextSearch
System.Windows.Controls..::.ViewBase
System.Windows.Data..::.CollectionContainer
System.Windows.Data..::.CollectionViewSource
System.Windows..::.Freezable
System.Windows.Ink..::.GestureRecognizer
System.Windows.Input..::.InputBinding
System.Windows.Media.Media3D..::.Visual3D
System.Windows.Media..::.Visual
System.Windows.Navigation..::.JournalEntry
System.Windows..::.TriggerAction
System.Windows..::.TriggerBase
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
Windows Vista
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0
参考
其他资源