C#DependencyProperty - 属性如何映射到对象

时间:2016-04-28 08:34:57

标签: c# dependency-properties internal

我对依赖属性进行了一些研究,并引起了一些混乱。 运行时如何确定DependencyProperty属于哪个对象?

想象一下所有注册相同DependencyProperty的对象集合(相同类型)。属性绑定到TextBox,TexBoxes显示在另一个下面。

首先:每个TextBox是否绑定到不同的DependencyProperty?我想是的,但我不是100%肯定。 第二:如果是这样,运行时如何确定DependencyProperty应该映射到的对象。注册DependencyProperty时,我只指定对象类型,例如MyClockControl。由于没有可靠的方法从被调用的方法中检索调用实例,我只是不知道如何解析“包含”该属性的对象,并在访问dependencyproperty时返回正确的多个属性。

来自http://www.wpftutorial.net/dependencyproperties.html

的示例
// Dependency Property
public static readonly DependencyProperty CurrentTimeProperty = 
     DependencyProperty.Register( "CurrentTime", typeof(DateTime),
     typeof(MyClockControl), new FrameworkPropertyMetadata(DateTime.Now));

// .NET Property wrapper
public DateTime CurrentTime
{
    get { return (DateTime)GetValue(CurrentTimeProperty); }
    set { SetValue(CurrentTimeProperty, value); }
}

感谢您的回答。

1 个答案:

答案 0 :(得分:1)

GetValueSetValue方法(DependencyObject)获取并设置在DependencyObject上声明的字典中的值(好......种类), 依赖属性是键。这意味着与DependencyProperty对应的值特定于DependencyObject的实例。