nimisha
nimisha

Reputation: 95

x:key keyword in WPF

Sometimes it seems that the Name and x:Name attributes are interchangeable. We can refer both Name and x:Name of a Framework element in XAML. But in case of x:key, WPF not providing "key" keyword like Name. Both Name and x:Name are for same purpose. Name would be available without typing x:. Why we are not able to use key like this in XAML?

Upvotes: 6

Views: 793

Answers (2)

Nitin Purohit
Nitin Purohit

Reputation: 18580

If you talk about Xaml then there is only the x:Name. WPF framework can map one of its properties to XAML's x:Name by using the RuntimeNamePropertyAttribute on the class which assign one of property to x:Name of Xaml. So setting x:Name is equivalent to setting Name.

x:Key is on the other hand is Xaml key to the resource. It is used to uniquely identify the resource in the ResourceDictionary.

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564413

There is actually a Name property on FrameworkElemnet. The x:Name Directive actually will map to the Name property because of a RuntimeNamePropertyAttribute that causes the parser to perform the mapping. These are actually not 100% interchangable - it's more of a one-way mapping, where the parser sets the FrameworkElement.Name property to match the value specified in x:Name.

No such property or attribute exists for "Key", so the x:Key directive can't map through to a property in the same way.

Upvotes: 6

Related Questions