Combobox Mouseover
Asked Answered
F

1

3

I would like to change the background (mouseover) color of my combobox when i move with a mouse over it.

I have read many post on stackoverflow and have tried a code like this:

<ComboBox.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
</ComboBox.Resources>

and thats the result:

enter image description here

But that's not what I wanted. I want to change this background:

enter image description here

How can I do that?

Fiddling answered 23/7, 2012 at 10:3 Comment(0)
T
2

Naive Solution: Just add brush to the resources and reference it from the Background property (via StaticResource binding) of the ComboBox:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <SolidColorBrush x:Key="BackgroundColorKey" Color="Red"/>
    <Style TargetType="{x:Type ComboBox}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{StaticResource BackgroundColorKey}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <ComboBox  Height="25"/>
</Grid>

Problem: After item is selected the background color is reset back to the default color. The only solution to fix this is to override the default template of the combobox.

Fix: Modify default ComboBox template. ComboBox default template is present on msdn. Here is the link - http://msdn.microsoft.com/en-us/library/ms752094(v=vs.100).aspx. See "ComboBox ControlTemplate Example" section.

Related links:

MouseOver highlighting style returning to default after a second (Caused by Aero?)

http://social.msdn.microsoft.com/Forums/en/wpf/thread/a18891e9-8879-4819-9679-247341782f60

Trackless answered 23/7, 2012 at 10:14 Comment(5)
ok we change the background, but i want that we change the background by mouseoverFiddling
Sorry, forgot about mouse over behavior. Then you need to add a style with trigger for IsMouseOver property. See my updated answer.Trackless
Yes I see. After item is selected the background color is reset back to the default color. The only solution to fix this is to override the default template of the combobox. See this - #251122.Trackless
ok thanks, is there a template which represents the combobox in the original state?Fiddling
You can find ComboBox default template on msdn.Here is the link - msdn.microsoft.com/en-us/library/ms752094(v=vs.100).aspx. See "ComboBox ControlTemplate Example" section.Trackless

© 2022 - 2024 — McMap. All rights reserved.