views:

33

answers:

1

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>?

+2  A: 

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.

Matt Greer
So when I am binding a collection of elements how would I make sure that each control gets a unique name? So I could have access to each control via a unique Name...
gmcalab
That is a good question that I can't reliably answer. It depends on how you are binding these items. You are allowed to set the Name property at runtime in code. But in XAML the Name property must be set explicitly. So after you bind the collection, you could probably iterate through all of the FrameworkElements that get generated and give them names at that point.
Matt Greer
@Matt Greer, yea that was the only I way I thought of was at runtime after binding run through all children nodes and set the Name Property. Do you know anything about the Binding Pipeline? Is there any event that is fired when binding has completed? Is there a way to tap into the Binding Pipeline to have a little more control over it?
gmcalab