Is there any difference (or advantage) for use this statement:
Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}"
with omitting the x:Key
attribute?
I think WPF assigns the key to the same x:Type
under the hood.
Is there any difference (or advantage) for use this statement:
Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}"
with omitting the x:Key
attribute?
I think WPF assigns the key to the same x:Type
under the hood.
The MSDN documentation for Style.TargetType
confirms your suspicions:
Setting the TargetType property to the TextBlock type without setting an x:Key implicitly sets the x:Key to {x:Type TextBlock}. This also means that if you give the above Style an x:Key value of anything other than {x:Type TextBlock}, the Style would not be applied to all TextBlock elements automatically. Instead, you need to apply the style to the TextBlock elements explicitly.
If the resource dictionary key of a style is a type, that style is used as the default style for all instances of that type that don't explicitly specify a style. Since the target type is usually supplied, the syntax of omitting the key is simply a shortcut for defining a default style.