Set Background in ComboboxItem on MouseOver does not work

Jim Jupiter 20 Reputation points
2024-04-01T09:05:00.9833333+00:00

Hi

I would like to change the background of an item in comboboxItem when the mouse is over.

But it does not work - see the code.

When I use e.g. "IsVisible" instead of "IsMouseOver" than it works (all items are yellow).

It seems that "IsMouseOver" is not triggert - but I don't know why - any ideas?

<Style x:Key="CBItemStyle" TargetType="ComboBoxItem">
            <Setter Property="Background" Value="#e7efda"/>
           
            <Setter Property="FontSize" Value="25"/>
            <Setter Property="FontFamily" Value="Yu Gothic UI Semibold"/>
            <Style.Triggers>
               
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Yellow"/>
                </Trigger>
            </Style.Triggers>
        </Style>

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,706 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,575 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 47,176 Reputation points Microsoft Vendor
    2024-04-01T09:55:44.73+00:00

    Hi,@Jim Jupiter . Welcome to Microsoft Q&A Forum.

    You could refer to the following example to set the background color when the mouse is hovering over a ComboboxItem.

    <Window.Resources>
    
         <Style x:Key="ItemStyleOne" TargetType="{x:Type ComboBoxItem}">
    
             <Setter Property="Foreground" Value=" black "/>
    
             <Setter Property="Template">
    
                 <Setter.Value>
    
                     <ControlTemplate TargetType="{x:Type ComboBoxItem}">
    
                         <Grid>
    
                             <Border x:Name="gd" Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}},Path=Background}" 
    
                                     BorderThickness="0" >
    
                                 <Grid>
    
                                     <ContentPresenter  Name="ContentSite" Margin="25, 3, 0, 11" VerticalAlignment="Center">
    
                                         
    
                                     </ContentPresenter>
    
                                 </Grid>
    
                             </Border>
    
                         </Grid>
    
                         <ControlTemplate.Triggers>
    
                             <Trigger Property="ComboBoxItem.IsMouseOver" Value="True">
    
                                 <Setter TargetName="gd" Property="Background" Value="green"></Setter>
    
                                 <Setter TargetName="gd" Property="TextElement.Foreground" Value="#ffffff"></Setter>
    
                              
    
                             </Trigger>
    
                          
    
                         </ControlTemplate.Triggers>
    
                     </ControlTemplate>
    
                 </Setter.Value>
    
             </Setter>
    
         </Style>
    
     </Window.Resources>
    
     <Grid>
    
         <ComboBox Height="24.2" Width="246.5" Margin="120,124,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"    ItemContainerStyle="{StaticResource ItemStyleOne}">
    
             <ComboBoxItem  Content="Always record"   ></ComboBoxItem>
    
             <ComboBoxItem Content="Record"  ></ComboBoxItem>
    
          
    
         </ComboBox>
    
     </Grid>
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful