Is there a difference in x: name and name for controls in xaml file?

I am new to Silverlight.
When I add some control to my xaml file with Visual Studio, it sets the name of the control with the Name property, but there is also x: Name.
Is there a difference and when to use each of them?
Thanks.

+6
source share
3 answers

short info

Yes, there is a difference. The bottom line is that x:Name can be used for elements of an object that do not have their own Name properties.

Longer explanation

You can only use Name for an element that represents an object that actually has a Name property. For example, everything that comes from FrameworkElement .

The x:Name attribute can be placed on any element that represents an object, regardless of whether this object has a Name property. If the object has the Name property, then it is assigned the value x:Name , so you cannot have both x:Name and Name on the same element.

When an object has the Name property or the x:Name property, the value of this property is associated with the recording of objects in the object tree. Through the tree of objects, the FindName method of the FindName object can find the object. FindName can find objects by name, even if this object does not have its own Name property, since it uses the name recorded in the object tree.

The auto-generated code for UserControl will contain field definitions for any element that has the Name or x:Name property. The FindName InitialiseComponent method will use the FindName method to assign values ​​to these fields.

Example

The above Xaml creates two LayoutRoot fields of type Grid and MyBrush type SolidColorBrush . If you were to change x:Name="LayoutRoot" to Name="LayoutRoot" , this will not change anything. Grid has a Name property. However, try changing x:Name="MyBrush" to Name="MyBrush" . This does not work because SolidColorBrush does not have a name property. With the above Xaml, you can make the code as follows: -

  public MainPage() { InitializeComponent(); MyBrush.Color = Colors.LightGray; } 

Open the InitializeComponent definition and take a look at the automatically generated code.

+12
source share

No, you just can't use them both. x: Name is what the XAML preprocessor actually uses, and Name is simply the convience property provided in the FrameworkElement class to set it.

From the MSDN link :

If the name is available as a property for an element, the name and x: name can be used interchangeably, but an error occurs if both attributes are specified in the same element.

+2
source share

Short answer: if you are writing material in XAML, it is best to just use x: Name sequentially.

Long answer: the previous answer mentioned that Name is a "convienience" property for accessing x: Name. It is right. However, now that the XAML tooling environment in both Visual Studio and Expression has really matured, and you see more and more XAML generated by the tools, you also probably see more and more x: Name, not Name. Tools prefer x: Name, because in this way they do not accept a somewhat risky dependency (potentially specific to the framework) re: like x: the name and name are actually the same, and they do not need a flip flop between setting Name if something turns out to be an element FrameworkElement and then x: Name on something like a storyboard and creating duality if you want to look at this XAML through something like the DOM. In other words, the Name attribute in XAML is indeed much less β€œusable” these days than would have been understood in the original API design. Part of the "convenience" was that there was no x: matching, but you should do it anyway for x: Class, and by now almost everyone is used to using the x: attributes and the general principles of XAML markup effectively.

I am not sure about the statement made by the original poster, which VS recommends using Name. Yes, the name is displayed as an intellisense option, but also x: Name. And all the cases that I see in templates where the object gets the initial name use x: Name, even most of them are FrameworkElements.

+1
source share

All Articles