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?
They are not the same thing.
x:Name
is a xaml concept, used mainly to reference elements. When you give an element the x:Name xaml attribute, "the specifiedx: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." (MSDN) So, it's a designer-generated field, which has internal access by default.Name
is the existing string property of aFrameworkElement
, listed as any other wpf element property in the form of a xaml attribute.As a consequence, this also means
x:Name
can be used on a wider range of objects. This is a technique to enable anything in xaml to be referenced by a given name.The only difference is that if you are using user Controls into a control from Same Assembly then Name will not identify your control and you will get an error " Use x:Name for controls in the same Assembly". So x:Name is the WPF versioning of naming controls in WPF. Name is just used as a Winform Legacy. They wanted to differentiate the naming of controls in WPF and winforms as they use attributes in Xaml to identify controls from other assemblies they used x: for Names of control.
Just keep in mind dont put a name for a control just for the sake of keeping it as it resides in memory as a blank and it will give you a warning that Name has been applied for a control buts its never used.