While defining style in resource dictionary you can use either
x:Name="xyz"
and
x:Type="xyz".
and can reference this style in XAML like {StaticResource xyz}.
Most examples use 'x:Key', and the difference between 'name' and 'key' is that using 'x:name' lets you use this style definition code-behind?
FIXES:
The question is totally wrong. What was intended to be asked was the difference between x:Key and x:Name. But didn't go trying this code myself, but was just relying on memories – thought I have both in ResourceDictionary, which was wrong. And I also didn't have such code in
<xxx.Resources >
sections, since it doesn't work either. You can't reference Style that doesn't have x:Key (x:Name doesn't work here), and adding two styles without x:Key throws exception since both get the same (empty?) key in dictionary.
Ray puts all the difference in a very nice way, thanks.
My fault
Best Answer
This is a trick question. In fact, you cannot define a style in a ResourceDictionary using either
or
Here is the difference:
x:Type="xyz"
is not valid XAML syntax.x:Name="xyz"
is actually valid XAML syntax that names an object (which affects the generation of code-behind) but does not provide a dictionary key.x:Key="xyz"
is also valid XAML syntax that provides a dictionary key but does not name an object.In a dictionary a key is required, so you must specify
x:Key
(except that for FrameworkTemplate and its subclasses the key can be inferred from the TargetType or DataType). In a dictionary you may also specifyx:Name
if desired but it does not affect the key.Note that
x:Type
is a markup extension that is predefined by XAML, whereasx:Name
andx:Key
are actual XAML keywords. Sox:Type
can only be used in markup extension syntax as the value of a property:whereas
x:Name
andx:Key
are attributes that can be used on elements.