Infosys Microsoft Alliance and Solutions blog

« Two-Tier ERP for the new business | Main | UI Prototyping with SketchFlow - A Viewpoint »

Silverlight - Using x:Key or x:Name

The other day I was discussing new features of Silverlight 3 and 4 with a colleague and we were talking about the ability of specifying styles in Silverlight 4 without using the x:Key attribute. To this my colleague responded that he has not been using x:Key at all, but always has used x:Name.

This sounded strange to me as this isn't the behavior I had seen with my working with WPF. So I did a bit of scanning around and found that this is a special behavior of Silverlight. Having migrated from WPF to Silverlight, I was always in the habit of writing resources with x:Key attribute and not x:Name. In Silverlight, if x:Key is missed out x:Name is used instead, to reference the resource. The following figure shows the relevant portion from the MSDN documentation


[Source: MSDN Documentation -]

The reason for this behavior, it seems is really to support how the storyboard resources were introduced in Silverlight 1.0 XAML. I believe (I haven't worked with Silverlight 1.0 and hence haven't really validated this), that at that time storyboards were referred to by x:Name so that they could be accessed in the java script code to start/stop them. If you know more about this, do add a comment. 

While this auto replacement between x:Key and x:Name works, as a best practice I would suggest that the usage of x:Name should be limited to those cases, where you want access to the object in question in code behind. If the object/resource in question is a pure XAML only resource (XAML and resource dictionaries), you should look at using x:Key. This is more consistent with the purpose of x:Key and x:Name and tomorrow if this behavior is changed, your code will not break. Do also note that the FindName API used to get access to XAML based instance objects in code behind, will not return any value if the instance in question is identified using an x:Key.

You could however be wanting to omit the x:Key value on a purpose, which would be using the new implicit style feature with Silverlight 4. However, in that case, don't mention the x:Name attribute as well, for if you do, the implicit style will not work.


The usage of x:Key and x:Name as far as my knowledge is concerned goes this way. When you want to access the element in XAML use x:Key while when you want to use the element in code behind use x:Name.
On top of this understanding, i have some different thought also. In current days we use design patterns like MVVM which actually leads to zero code in code behind. This means you will never need x:Name and you continue writing code using x:Key

Shashikant, good point on MVVM.

For <Style... with x:Name instead of x:Key,

Resharper 5.0 has false-negative error messages stating it cannot find the resource. So, use x:Key for resources.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter