有什么区别呢?
TargetType="{x:Type Button}"
和
TargetType="Button"
有什么区别呢?
TargetType="{x:Type Button}"
和
TargetType="Button"
XAML 设计器应用内置类型转换器,将字符串值 "Button" 转换为 System.Type
,即 Button
,使其看起来似乎没有实际区别。
然而,应该练习使用显式的 Type 指定,使用 x:Type
。
在使用 BasedOn
继承样式时需要使用显式的 Type
指定,隐式字符串 Type
将不起作用。
例如:
这样可以工作:
BasedOn="{StaticResource {x:Type Button}}"
但不是这个...
BasedOn="{StaticResource Button}"
因为在此处它将尝试搜索关键字为“Button”的资源。但是在x:Type
规范中,由于我们已经明确指定了Button
Type
,静态资源的搜索将会发生在针对Button
的Style
上。
Type
对象的目标,该对象类型为{x:Type Button}
;而implicit则表示字符串"Button"。 - WPF-it