gpt4 book ai didi

wpf - x :Key & TargetType in styles

转载 作者:行者123 更新时间:2023-12-04 01:32:45 25 4
gpt4 key购买 nike

使用此语句是否有任何区别(或优势):

Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}"

省略 x:Key属性?

我认为 WPF 将 key 分配给相同的 x:Type在引擎盖下。

最佳答案

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.



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

关于wpf - x :Key & TargetType in styles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8108923/

25 4 0