Search code examples
wpfcomboboxmouseoverstyling

Delete MouseOver Effect on custom Combobox style wpf


In my wpf document I have made a custom Combobox style like so:

<ComboBox Background="#222222" BorderBrush="Black" Grid.Column="1" Height="30" Width="250">
<ComboBox.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#262626" />
</ComboBox.Resources>
<ComboBox.ItemContainerStyle>
    <Style TargetType="{x:Type ComboBoxItem}">
        <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="#262626" /> </Trigger> </Style.Triggers>
    </Style>
</ComboBox.ItemContainerStyle>
<ComboBoxItem Foreground="White" Name="Item1">Item1

</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item2">Item2</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item3">Item3</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item4">Item4</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item5">Item5</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item6">Item6</ComboBoxItem>

But whenever I hover over one of the ComboboxItems, I still get the default hover colour, so how do I remove it and place my colour?

I am relatively new to coding so any help would be appreciated.


Solution

  • You have to override the default ControlTemplate of the ComboBoxItem in order to override the default visual state triggers.
    Check Microsoft Docs: Control Styles and Templates to see the default Style implementation of framework controls.

    ComboBoxItem Style

    <ComboBox>
      <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="ComboBoxItem">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}">
                  <ContentPresenter />
                </Border>
    
                <ControlTemplate.Triggers> 
                  <Trigger Property="IsMouseOver" 
                           Value="True"> 
                    <Setter Property="Background" 
                            Value="#262626" /> 
                  </Trigger> 
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Setter.Value>
          <Setter>
        </Style>
      </ComboBox.ItemContainerStyle>
    </ComboBox>