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

139 Views Asked by At

I'm hoping someone can explain some unexpected behaviour I have come across whilst continuing my exploration of DependencyObjects and DependencyProperties.

Given the following simple class:

    class SomeClass : DependencyObject {
    }

I can happily write code such as:

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 output:

0
3.24

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

Does anyone else find this strange behaviour? I would have expected some form of exception along the lines of "You have not registered this property with this object"... I would appreciate any guidence as to whether there is any particular use for adding DPs to DOs in this way, as I cannot see the purpose of allowing this behaviour.

Many thanks, Matt

1

There are 1 best solutions below

0
On

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