WPF Combobox style and triggers do not work

I have a combobox:

var cmbLogin = new ComboBox()
{
    Width = 200,
    Height = 50,
    Margin = new Thickness(20),
    HorizontalContentAlignment = HorizontalAlignment.Center,
    Background = Brushes.Transparent,
    Foreground = Brushes.White,
    Focusable = true,
};

cmbLogin.Items.Add("AAAAA");
cmbLogin.Items.Add("BBBBB");

Now I want to define the style and triggers:

Style cmbStyle = new Style(typeof(ComboBox));
cmbStyle.Setters.Add(new Setter(BackgroundProperty, Brushes.Green));
cmbStyle.Setters.Add(new Setter(ForegroundProperty, Brushes.Red));


Trigger t1 = new Trigger { Property = ComboBox.IsMouseOverProperty, Value = true };
t1.Setters.Add( new Setter(ComboBox.BackgroundProperty, Brushes.Yellow));
cmbStyle.Triggers.Add(t1);
cmbLogin.Style = cmbStyle;

but the effect with and without a mouse will always be the same as before

enter image description here enter image description here

thank

+4
source share
1 answer

It seems to me that your problem is not in the Code-Behind approach, but in using the wrong Property. Your desired / expected interface will not happen even if you use XAML.

The ComboBox Background property is not a property you need.

Just to make the first conclusion clear: If you tried to change the foreground instead of the background, it would work well and change the foreground of the text.

, , ComboBox . : Change-background-of-WPF-Combobox customizing-wpf-combo-box-style

,

+3

Source: https://habr.com/ru/post/1667360/


All Articles