Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There really is only one name in XAML, the <code>x:Name</code>. A framework, such as WPF, can optionally map one of its properties to XAML's <code>x:Name</code> by using the <code>RuntimeNamePropertyAttribute</code> on the class that designates one of the classes properties as mapping to the x:Name attribute of XAML.</p> <p>The reason this was done was to allow for frameworks that already have a concept of "Name" at runtime, such as WPF. In WPF, for example, <code>FrameworkElement</code> introduces a Name property.</p> <p>In general, a class does not need to store the name for <code>x:Name</code> to be useable. All <code>x:Name</code> means to XAML is generate a field to store the value in the code behind class. What the runtime does with that mapping is framework dependent.</p> <p>So, why are there two ways to do the same thing? The simple answer is because there are two concepts mapped onto one property. WPF wants the name of an element preserved at runtime (which is usable through Bind, among other things) and XAML needs to know what elements you want to be accessible by fields in the code behind class. WPF ties these two together by marking the Name property as an alias of x:Name.</p> <p>In the future, XAML will have more uses for x:Name, such as allowing you to set properties by referring to other objects by name, but in 3.5 and prior, it is only used to create fields.</p> <p>Whether you should use one or the other is really a style question, not a technical one. I will leave that to others for a recommendation.</p> <p>See also <a href="https://stackoverflow.com/questions/4605777/automationproperties-name-vs-xname">AutomationProperties.Name VS x:Name</a>, AutomationProperties.Name is used by accessibility tools and some testing tools.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload