需要设置ItemContainerStyle,在里面加上Triggers
首先定义模板
<Style TargetType="{x:Type ComboBoxItem}" x:Key="MyComboBoxItemStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border x:Name="Bd">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="#008aec"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="Bd" Value="#008aec"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后应用样式
<Style TargetType="{x:Type ComboBox}" x:Key="MyComboBoxStyle">
<Setter Property="ItemContainerStyle" Value="{StaticResource MyComboBoxItemStyle}"></Setter>
</Style>
本文介绍了如何在WPF中更改ComboBox选中项的颜色。通过设置ItemContainerStyle并添加Triggers,可以实现对选中项样式的自定义,详细步骤包括定义模板和应用样式。
1236

被折叠的 条评论
为什么被折叠?



