3

I get this error when I define my attached dependency properties in a class outside the class hierarchy and set the owner to a common parent class.

Attached dependency property in WindowBase class (outside class hierarchy => generated error):

public static readonly DependencyProperty AreaColorProperty =
DependencyProperty.RegisterAttached("AreaColor", typeof(AreaColor), typeof(Window));

TemplateBinding that fails

{TemplateBinding local:WindowBase.AreaColor}

If I instead define the attached dependency property in a class within the class heirarchy and set the owner to this class, then I don't get any errors, why is this?

Attached dependency property in WindowBase (within class hierarchy => no errors):

public static readonly DependencyProperty AreaColorProperty =
DependencyProperty.RegisterAttached("AreaColor", typeof(AreaColor), typeof(WindowBase));

Best Regards, Jesper

Krimson
  • 762
  • 6
  • 25
  • 1
    Of course the problem is that the TemplateBinding should be: {TemplateBinding Window.AreaColor}. Although this generated a new error: Cannot find the static member 'AreaColorProperty' on the type Window. This I can then work around by specifying a converter on the TemplateBinding... Hmmm – Krimson Apr 12 '10 at 08:16

1 Answers1

2

I solved the issue by switching to relative source binding:

{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=(Window.CaseAreaColor)}
Krimson
  • 762
  • 6
  • 25
  • This also acts as a solution to a broken TemplateBinding using a property and converter which otherwise doesn't throw the "Cannot convert the value in attribute 'Property' to object of type 'System.Windows.DependencyProperty' error if a converter is not used. – Sphynx Oct 27 '17 at 19:58