本文介绍了TargetType=“{x:Type Button}"有什么区别?和 TargetType=“按钮"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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=“按钮"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:55