使用此声明有什么区别(或优点):
Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}"
省略x:Key属性?
我认为WPF将密钥分配给相同的x:类型在引擎盖下.
解决方法
Style.TargetType
的MSDN文档证实您的怀疑:
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.
如果一个样式的资源字典键是一个类型,该样式将被用作该类型的所有实例的默认样式,该类型不会明确指定样式.由于通常提供目标类型,省略密钥的语法只是定义默认样式的快捷方式.