DependencyObject 类
This page is specific to:.NET Framework Version:
.NET Framework 类库
DependencyObject 类

更新:2007 年 11 月

表示一个参与依赖项属性系统的对象。

命名空间:  System.Windows
程序集:  WindowsBase(在 WindowsBase.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/xaml/presentation

语法

public class DependencyObject extends DispatcherObject
<DependencyObject .../>
备注

DependencyObject 类对它的许多派生类启用 Windows Presentation Foundation (WPF) 属性系统服务。

属性系统的主要功能是计算属性的值,并提供有关值已更改的系统通知。参与属性系统的另一个关键类是 DependencyPropertyDependencyProperty 可实现在属性系统中注册依赖项属性,并提供有关每个依赖项属性的标识和信息,而 DependencyObject 作为一个基类则使对象能够使用依赖项属性。

DependencyObject 包括以下服务和特征:

  • 依赖项属性宿主支持。通过调用 Register 方法并将该方法的返回值作为公共静态字段存储在类中,即可注册依赖项属性。

  • 附加属性宿主支持。通过调用 RegisterAttached 方法并将该方法的返回值作为公共静态只读字段存储在类中,即可注册附加属性。(还有一些附加成员要求;请注意,这代表附加属性的 WPF 特定实现。有关详细信息,请参见附加属性概述。) 然后,可以在从 DependencyObject 派生的任何类上设置附加属性。

  • 存在于 DependencyObject 上的任何依赖项属性值的 get、set 和 clear 实用工具方法。

  • 依赖项属性或附加属性的元数据、强制值支持、属性更改通知以及重写回调。同时,DependencyObject 类简化了依赖项属性的每所有者属性元数据。

  • 派生自 ContentElementFreezableVisual 的类的公共基类。(另一个基元素类 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
另请参见

参考

其他资源

© 2010 Microsoft Corporation 版权所有。   保留所有权利 | 商标 | 隐私权声明
Page view tracker
为轻量型库评级
x
依无脚本原则生成的轻量型库 (loband),添加了大家要求的功能:搜索框和默认代码语言选择。
您喜欢这个搜索框吗?
您喜欢标签式代码块吗?
此主题有用吗?
提供详细反馈。
谢谢
x
感谢您帮助改善 MSDN Online。
反馈意见
切换视图
经典视图
轻量型视图
无脚本视图
切换视图