【问题标题】:Change foreground color of ComboBoxItem when ComboBox is hovered当 ComboBox 悬停时更改 ComboBoxItem 的前景色
【发布时间】:2015-01-14 21:15:34
【问题描述】:

目前,我正在设计 WPF 控件的样式。这就是我的自定义 <Button> 看起来悬停的样子。

这就是<ComboBox> 鼠标悬停时的样子。

简单地说,当整个 ComboBox 悬停时,我希望能够更改当前选定的 ComboBoxItem 的前景色。在此示例中,我希望在 <Button> 控件上具有类似的黄色。

问题是,<ComboBox> 对 ComboBox 项和切换按钮有不同的 ControlTemplates。我不知道这两者之间如何互动。

<ComboBox> 的模板很大,所以我不会在这里发布。此外,如果不需要解释问题,您不必发布完整的答案。

【问题讨论】:

  • 你不能在触发器中添加另一个 setter 吗?
  • 就像@Okuma.Scott 说的那样,只需在您的 IsHighlighted 或其他触发器中输入<Setter Property="TextElement.Foreground" Value="Yellow" /> 即可,或者您可以只使用read the docs
  • "的模板很大,这里就不贴了"想要代码答案,把代码贴在MCVE后面

标签: c# wpf xaml combobox


【解决方案1】:

好的,我找到了解决方案。您需要编辑默认的 ComboBox 模板。

在 Visual Studio 2013 中,您可以通过在 ComboBox 上单击鼠标右键 -> 编辑模板 -> 编辑副本在 Designer 中获取默认模板...

<ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}"> 中,您必须添加以下触发器:

<ControlTemplate.Triggers>
...
<Trigger Property="IsMouseOver" TargetName="toggleButton" Value="true">
    <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="Yellow" />
</Trigger>
...
</ControlTemplate.Triggers>

另外,对 ComboBoxItem 应用样式:

<Style TargetType="{x:Type ComboBoxItem}">
    <Setter Property="Foreground" Value="Black"/>
</Style>

现在,它就像一个魅力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    相关资源
    最近更新 更多