以编程方式添加样式触发器

时间:2010-07-07 09:23:25

标签: wpf triggers

任何人都可以帮助您以编程方式添加以下样式:

<style>
 <style.Triggers>
     <Trigger Binding="{Binding CustomerId}" Value ="1"/>
     <setter Property="Background" Value="Red"/>
 </style.Triggers>
</style>

1 个答案:

答案 0 :(得分:13)

你的XAML不正确,但我想你想看到这个:

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