The title says it all. Sometimes it seems that the Name
and x:Name
attributes are interchangeable.
So, what are the definitive differences between them, and when is it preferable to use one over the other?
Are there any performance or memory implications to using them the wrong way?
There really is only one name in XAML, the
x:Name
. A framework, such as WPF, can optionally map one of its properties to XAML'sx:Name
by using theRuntimeNamePropertyAttribute
on the class that designates one of the classes properties as mapping to the x:Name attribute of XAML.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,
FrameworkElement
introduces a Name property.In general, a class does not need to store the name for
x:Name
to be useable. Allx:Name
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.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.
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.
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.
See also AutomationProperties.Name VS x:Name, AutomationProperties.Name is used by accessibility tools and some testing tools.
My research is
x:Name
as global variable. However,Name
as local variable. Does that mean x:Name you can call it anywhere in your XAML file but Name is not.Example:
You can't
Binding
propertyContent
ofButton
with Name is "btn" because it outsideStackPanel
X:Name can cause memory issues if you have custom controls. It will keep a memory location for the NameScope entry.
I say never use x:Name unless you have to.
The specified x:Name becomes the name of a field that is created in the underlying code when XAML is processed, and that field holds a reference to the object. In Silverlight, using the managed API, the process of creating this field is performed by the MSBuild target steps, which also are responsible for joining the partial classes for a XAML file and its code-behind. This behavior is not necessarily XAML-language specified; it is the particular implementation that Silverlight applies to use x:Name in its programming and application models.
Read More on MSDN...
Name:
x:Name:
Using both directives in XAML for one FrameworkElement or FrameworkContentElement will cause an exception: if the XAML is markup compiled, the exception will occur on the markup compile, otherwise it occurs on load.
I always use the x:Name variant. I have no idea if this affects any performance, I just find it easier for the following reason. If you have your own usercontrols that reside in another assembly just the "Name" property won't always suffice. This makes it easier to just stick too the x:Name property.