user4367090
user4367090

Reputation:

WPF Combobox style and triggers not working

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 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 mouse is always the same as before

enter image description here enter image description here

thanx

Upvotes: 3

Views: 1456

Answers (1)

Oran Gerbovski
Oran Gerbovski

Reputation: 319

It seems to me that your issue is not the Code-Behind approach, rather the usage of the wrong Property. Your Desired/Expected UI would not have happen even if you would use XAML.

ComboBox Background property is not the property you need.

Just to make the first point clear: If you would have tried to change the foreground instead of Background it would have worked well and change the text foreground.

But unfortunately to change your ComboBox Background or Highlight you need to work a bit harder. And here is a good explanation: Change-background-of-WPF-Combobox customizing-wpf-combo-box-style

Hope it helps

Upvotes: 3

Related Questions