wpf TargetType="{x:Type Button}" 和 TargetType="Button" 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13558287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:21:40  来源:igfitidea点击:

What is the difference for TargetType="{x:Type Button}" and TargetType="Button"?

wpfsilverlightxaml

提问by DEN

What is the difference for

有什么区别

TargetType="{x:Type Button}"

and

TargetType="Button"

采纳答案by WPF-it

The XAML designer applies inbuilt type converters that convert the string value "Button" to System.Typewhich is Button, which makes it seem like there is no practical difference.

XAML 设计器应用内置类型转换器将字符串值 "Button" 转换System.Type为 is Button,这使得看起来没有实际区别。

However one should practise to use the explicit Type specification using x:Type.

但是,应该练习使用显式类型规范使用x:Type.

Explicit Typespecification is required is when we inherit Stylesusing BasedOn, there implicit string Typewont work.

Type需要显式规范是当我们继承Stylesusing 时BasedOn,隐式字符串将Type不起作用。

e.g.

例如

This would work

这会工作

 BasedOn="{StaticResource {x:Type Button}}"

But not this...

但不是这个...

 BasedOn="{StaticResource Button}"

as here it would try to search a resource with Key "Button". But in the x:Typespecification, as we already specified explicit ButtonTypethe search of the static resource would be happen for the Stylewhich is targetted for a Button.

就像这里一样,它会尝试使用“按钮”键搜索资源。但是在x:Type规范中,正如我们已经明确指定ButtonType的那样,静态资源的搜索将Style针对Button.