Why can I access DependencyProperties that are not registered with my DependencyObject?

I hope someone can explain some unexpected behavior that I encountered while continuing to explore DependencyObjects and DependencyProperties.

Given the following simple class:

class SomeClass : DependencyObject { } 

I can happily write code, for example:

 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)); } 

which gives the following result:

 0 3.24 

There is nothing in my class that has anything to do with the EllipseGeometry class, and I did not add my class as the owner of the EllipseGeometry.RadiusXProperty property or used RegisterAttached (), so why does this work? It seems I can happily add any DP to my DO without the Framework raising an error.

Does anyone else find this weird behavior? I would expect some form of exception according to “You did not register this property with this object” ... I would appreciate any opinion on whether there is any specific use for adding DP to DOs in this way, since I does not see the goal of resolving this behavior.

Thanks a lot Matt

+8
c # wpf dependency-properties dependencyobject
source share
1 answer

This behavior allows you to use attached properties. Without this, how can you use Grid.Row in a TextBox, for example.

+1
source share

All Articles