WPF: Unable to set MouseOver effect for ComboBox
-
Hi, I have a Combobox, who gets Gradient blue color when mouse-over or clicked. I managed to change the color of selecting Items background, but the mouse over background color couldn't change.
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ComboBox}"> <Border Name="bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Background}" Padding="2"> <ContentPresenter /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="bd" Property="Background" Value="#F7F7F7" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter>
On adding the above code, MouseOver is definetely achieved i.e. Background color is changed on MouseOver. But with this, I can't see the drop down triagle icon or click the combobox that shows the drop down. What am I lacking and ho to achieve it ? Any help is highly appreciated.
Thanks & Regards,
-
Hi, I have a Combobox, who gets Gradient blue color when mouse-over or clicked. I managed to change the color of selecting Items background, but the mouse over background color couldn't change.
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ComboBox}"> <Border Name="bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Background}" Padding="2"> <ContentPresenter /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="bd" Property="Background" Value="#F7F7F7" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter>
On adding the above code, MouseOver is definetely achieved i.e. Background color is changed on MouseOver. But with this, I can't see the drop down triagle icon or click the combobox that shows the drop down. What am I lacking and ho to achieve it ? Any help is highly appreciated.
Thanks & Regards,
When you set the Template property, you're basically replacing the entire control. In this case, you turned a ComboBox into a ContentControl by removing (not including) all of the things that made it a ComboBox. If all you want to change is the Background, then you should be putting this stuff directly in the Style.Triggers, not inside a Template. For example:
<Style TargetType="ComboBox">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)