我希望有人可以解释我在继续探索DependencyObjects和DependencyProperties时遇到的一些意想不到的行为。
给出以下简单类:
class SomeClass : DependencyObject {
}
我很乐意写代码如下:
public static void Test() {
SomeClass a = new SomeClass();
Console.WriteLine(a.GetValue(EllipseGeometry.RadiusXProperty));
a.SetValue(EllipseGeometry.RadiusXProperty, 3.24 );
Console.WriteLine(a.GetValue(EllipseGeometry.RadiusXProperty));
}
给出以下输出:
0
3.24
我的类中没有任何与EllipseGeometry类有任何关系的东西,我没有将我的类添加为EllipseGeometry.RadiusXProperty属性的所有者或者使用了RegisterAttached(),为什么这样做呢?在没有框架引发错误的情况下,我似乎可以很高兴地将任何DP添加到我的DO中。
有没有其他人发现这种奇怪的行为?我本来期望某种形式的例外,因为“你还没有用这个对象注册这个属性”......我很感激任何指导是否有任何特殊用途以这种方式向DO添加DP,因为我看不到允许这种行为的目的。
非常感谢, 马特
答案 0 :(得分:1)
此行为允许您使用附加属性。如果没有这个,你怎么能在TextBox上使用Grid.Row。