[ACCEPTED]-What is the Difference Between x:Key, x:Name, and x:UID in a DataTemplate in WPF?-uid

Accepted answer
Score: 45

The 'x:' specifies the namespace, which 32 would in your case most likely be "http://schemas.microsoft.com/winfx/2006/xaml" You 31 will see the alias declared at the top of 30 your Window.Xaml file. x:Key, x:Name, etc 29 are all directives in that namespace.

In 28 contrast, the 'Name' attribute (without 27 the x:) is a dependency property declared 26 in the FrameworkElement class.


Uniquely identifies 25 elements that are created and referenced 24 in a XAML-defined dictionary. Adding an 23 x:Key value to a XAML object element is 22 the most common way to identify a resource 21 in a resource dictionary, for example in 20 a WPF ResourceDictionary.


Uniquely identifies 19 XAML-defined elements in a XAML namescope. XAML 18 namescopes and their uniqueness models can 17 be applied to the instantiated objects, when 16 frameworks provide APIs or implement behaviors 15 that access the XAML-created object graph 14 at run time.


Provides a unique identifier 13 for markup elements. In many scenarios, this 12 unique identifier is used by XAML localization 11 processes and tools.


I have only seen x:Uid when 10 a app must support different languages with 9 a resource dictionary.

For the other two 8 (x:Key and x:Name), a basic rule of thumb 7 is to use x:Name for Framework elements and x:Key for 6 styles, templates, and so on. So for your 5 question, if you are naming a template itself, you 4 would use the x:Key directive. Controls declared 3 within the template would use the x:Name directive.

A 2 complete list of all Xaml directives is 1 given at Xaml Namespace

Score: 6

If you want to apply the template to all 18 the tabs in your page, you can use x:Type, but 17 if you want to apply it to few tabs and 16 not to all the tabs you can use x:Key.

Generally 15 you will use x:Key when you want to use it as 14 StaticResource in your xaml file. You will 13 provide x:Name to a control or template when you 12 want to refer it in your code-behind. I 11 have never used X:Uid, but this is what 10 MSDN says,

Use x:Uid to identify an object 9 element in your XAML. Typically this object 8 element is an instance of a control class 7 or other element that is displayed in 6 a UI. The relationship between the string 5 you use in x:Uid and the strings you use 4 in a resources file is that the resource 3 file strings are the x:Uid followed by a 2 dot (.) and then by the name of a specific 1 property of the element that's being localized.

More Related questions