Wednesday, February 4, 2009

Binding to XAML declared types with non-default constructors using ElementName binding syntax

Some of our XAML files were using Element name binding like this Binding="{Binding ElementName=Window}" to bind to the class itself.  While refactoring and adding a non default constructor Visual Studio complains: "The type CLASSNAME cannot have a Name attribute. Value types and types without a default constructor can be used as items within a ResourceDictionary."

For most of the day I changed class bindings to use DataContext = this in the constructor or FindAncestor calls at the binding declaration.  Late in the day I ran into an infinite recursion problem.  Not wanting to diagnose it I found an MSDN article pointing me to use RegisterName("Window", this); in the constructor after InitializeComponent() to work around the issue.