简体   繁体   中英

Binding a UIElement's Name

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM