wpf – x:样式中的Key和TargetType

使用此声明有什么区别(或优点):

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.

如果一个样式的资源字典键是一个类型,该样式将被用作该类型的所有实例的默认样式,该类型不会明确指定样式.由于通常提供目标类型,省略密钥的语法只是定义默认样式的快捷方式.

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...