【问题标题】:What is the difference for TargetType="{x:Type Button}" and TargetType="Button"?TargetType="{x:Type Button}" 和 TargetType="Button" 有什么区别?
【发布时间】:2017-07-21 02:15:07
【问题描述】:

有什么区别

TargetType="{x:Type Button}"

TargetType="Button"

【问题讨论】:

标签: wpf silverlight xaml


【解决方案1】:

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

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

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

例如

这样就可以了

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

但不是这个……

 BasedOn="{StaticResource Button}"

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

【讨论】:

  • 感谢您的回复!但是你说的明确的风格是什么意思?
  • 显式是Type {x:Type Button}的对象,隐式是字符串"Button"
猜你喜欢
  • 2010-11-08
  • 2021-06-07
  • 2010-11-26
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多