问题描述
与
TargetType="{x:Type Button}"
和
TargetType="Button"
推荐答案
XAML 设计器应用内置类型转换器将字符串值Button"转换为 System.Type
,即 Button
,这使得它看起来没有实际区别.
The XAML designer applies inbuilt type converters that convert the string value "Button" to System.Type
which is Button
, which makes it seem like there is no practical difference.
然而,应该练习使用 x:Type
来使用显式类型规范.
However one should practise to use the explicit Type specification using x:Type
.
显式Type
规范是当我们使用BasedOn
继承Styles
时,隐式字符串Type
不起作用.
Explicit Type
specification is required is when we inherit Styles
using BasedOn
, there implicit string Type
wont work.
例如
这行得通
BasedOn="{StaticResource {x:Type Button}}"
但不是这个...
BasedOn="{StaticResource Button}"
在这里,它会尝试使用按钮"键搜索资源.但是在 x:Type
规范中,因为我们已经明确指定了 Button
Type
静态资源的搜索将发生在 以
.Button
为目标的样式
as here it would try to search a resource with Key "Button". But in the x:Type
specification, as we already specified explicit Button
Type
the search of the static resource would be happen for the Style
which is targetted for a Button
.
这篇关于TargetType=“{x:Type Button}"有什么区别?和 TargetType=“按钮"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!