我在 WPF-MVVM 中有一个组合框,我已经通过更改组合框的弹出框和文本框来设置组合框的样式。
当我滚动组合框列表项时,他们的背景是粉红色的,这就是我更改的内容。但是从组合框列表中选择一个项目后,组合框项目中的选定值具有蓝色背景。这是 Windows 窗体和 WPF 中组合框的默认值。
查看图片了解更多详情。
如何更改组合框文本框控件中选定文本的背景颜色
组合框有
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 上测试但应该可以工作)
另一种方法是设置 ComboBox
的 ItemContainerStyle
属性,并根据当前 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/