Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
2.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

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);  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...