Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add style trigger

Tags:

wpf

triggers

Can anyone please help how to programatically add the following style:

<style>
 <style.Triggers>
     <Trigger Binding="{Binding CustomerId}" Value ="1"/>
     <setter Property="Background" Value="Red"/>
 </style.Triggers>
</style>
like image 952
user384080 Avatar asked Jul 07 '10 09:07

user384080


1 Answers

Your XAML is incorrect, but I guess you want to see this:

Style st = new Style();

DataTrigger tg = new DataTrigger()
{
    Binding = new Binding("CustomerId"),
    Value = 1
};

tg.Setters.Add(new Setter()
{
    Property = Control.BackgroundProperty,
    Value = ColorConverter.ConvertFromString("Red")
});

st.Triggers.Add(tg);  
like image 130
Andrey Avatar answered Oct 21 '22 21:10

Andrey