DEN*_*DEN 51 silverlight wpf xaml
有什么区别
TargetType="{x:Type Button}"
Run Code Online (Sandbox Code Playgroud)
和
TargetType="Button"
Run Code Online (Sandbox Code Playgroud)
WPF*_*-it 45
我想没有区别,因为XAML设计器应用内置类型转换器将字符串值"Button"转换System.Type为Button.
但是应该练习使用显式类型规范x:Type.
Type需要显式指定的另一个地方是当我们继承Styles使用时BasedOn,隐式字符串Type不起作用.
例如
这会奏效
BasedOn="{StaticResource {x:Type Button}}"
Run Code Online (Sandbox Code Playgroud)
但不是这个......
BasedOn="{StaticResource Button}"
Run Code Online (Sandbox Code Playgroud)
在这里它将尝试使用键"按钮"搜索资源.但是在x:Type规范中,正如我们已经明确指出Button Type的那样,静态资源的搜索将发生在Style针对a的目标上Button.