c# - 如何更改 WPF ComboBox SelectedText 背景颜色?

标签 c# .net wpf xaml mvvm

我在 WPF-MVVM 中有一个组合框,我已经通过更改组合框的弹出框和文本框来设置组合框的样式。

当我滚动组合框列表项时,他们的背景是粉红色的,这就是我更改的内容。但是从组合框列表中选择一个项目后,组合框项目中的选定值具有蓝色背景。这是 Windows 窗体和 WPF 中组合框的默认值。

查看图片了解更多详情。

enter image description here

如何更改组合框文本框控件中选定文本的背景颜色

组合框有

IsEditable=True 属性集

最佳答案

你可以这样做:

<ComboBox.Resources>
    <!--Selected color when the ComboBox is focused-->
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />
    <!--Selected color when the ComboBox is not focused-->
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow" />

    <!--selected text-->
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Yellow" />
</ComboBox.Resources>

(在 ListBox 上测试但应该可以工作)

另一种方法是设置 ComboBoxItemContainerStyle 属性,并根据当前 ComboBoxItem 选择状态触发:

<ComboBox>
  <ComboBox.Resources>
    <Style TargetType="TextBlock">
      <Style.Triggers>
        <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" Value="True">
          <Setter Property="Foreground" Value="White" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </ComboBox.Resources>
  <ComboBox.ItemContainerStyle>
    <Style TargetType="ComboBoxItem" x:Key="ContainerStyle">
      <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
          <Setter Property="Background" Value="Red" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </ComboBox.ItemContainerStyle>    
</ComboBox>

关于c# - 如何更改 WPF ComboBox SelectedText 背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9905919/

相关文章:

c# - WPF:动画线减慢 UI

c# - 搜索框中的结果建议未显示图片

c# - 当 xml 缩进时,XMLREADER 给出不同的结果

c# - 如何在 Windows Phone 8 中异步 append 文件

c# - File.ReadLines(filePath).First() 是否立即关闭文件?

c# - 正确分割 System.Windows.Media.PathGeometry

C# 自动引用分配 - 使引用为空

javascript - 自定义 validationSummary HTML 帮助程序在客户端不起作用

c# - 如何在 C# Windows 窗体应用程序中将焦点发送到 tabindex 低于当前控件的控件?

WPF DataGrid,添加行时应用程序崩溃