Is there any way to use binding to set the UIElement’s name?
I always get a runtime exception saying
System.Windows.Markup.XamlParseException:
AG_E_UNKNOWN_ERROR
<Button Name="{Binding myName}" Content="{Binding myName, Mode=TwoWay}" />
If I take the binding off for the Name property the control works and the Content property is binded successfully.
Can you not bind to the Name Property of an <UIElement>?
No, you cannot bind to
Name. It is a dependency property like anything else, but with restrictions. See MSDN (which has a section that talks about naming things at runtime)Name has restrictions on what is a legal name, and the XAML compiler has no way of knowing what the name is if you are giving the name at runtime. Plus the compiler needs to generate the code behind file, and it uses the Name property to name the reference in the code, so it must be known at compile time.