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.

4 comments:

Anonymous said...

Thanks so much! This worked pretty well and your post saved me some time investigating on this issue :-)

Anonymous said...

Amiable post and this enter helped me alot in my college assignement. Gratefulness you seeking your information.

Instant Development said...

Thanks. In the same boat with refactoring some views to common base classes I was stuck with this error. This was an easy way to fix it.

Anonymous said...

Nice brief and this fill someone in on helped me alot in my college assignement. Gratefulness you on your information.