【问题标题】:Using Style for ComboBox and TextBox为 ComboBox 和 TextBox 使用样式
【发布时间】:2011-05-04 10:24:41
【问题描述】:

有没有办法我可以将样式重用超过 1 个TargetType,例如。组合框和文本框

<Style TargetType="ComboBox, TextBox" />

有这样的事吗?还是唯一的方法是复制样式并将每种样式定位到不同的类型?

【问题讨论】:

标签: wpf


【解决方案1】:

你不能(如果我没记错的话)。 但是为了避免复制粘贴,您可以做的是创建一个带有键的 BaseStyle,然后为 ComboBox 和 TextBox 创建两个样式,它们是基于 BaseStyle 的。像这样的:

<Style x:Key="BaseStyle" TargetType="{x:Type Control}">
   <Setter ... />
</Style>

<Style BasedOn="{StaticResource BaseStyle }" TargetType="{x:Type ComboBox }" />
<Style BasedOn="{StaticResource BaseStyle }" TargetType="{x:Type TextBox}" />

【讨论】:

    【解决方案2】:

    除此之外,您可以提供 targettype 作为控件并定义控件的 setter 属性并将该样式应用于文本框和组合框。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 2018-05-24
      • 1970-01-01
      • 2011-12-22
      • 2016-11-21
      • 2018-06-07
      • 1970-01-01
      相关资源
      最近更新 更多