Reputation: 2593
I try to Bind a DataGridRow Trigger to set the background to red if my item is critical. Otherwise, I want the AlternatingRowBackground
. Here is what I have so far:
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsCritical}" Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</ControlTemplate.Triggers>
It works fine, but it disable the AlternatingRowBackground
(I read somewhere that it's normal). So I want to do it manually and set the row background myself with that :
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="0">
<Setter Property="Background" Value="White" />
</Trigger>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="WhiteSmoke" />
</Trigger>
</Style.Triggers>
But by doing that it never become red (The Critical binding trigger). I have tried MultiDataTrigger
and have both condition (IsCritical -> False && Alternate) but the binding fails. I also tried to put both trigger in Style
and ControlTemplate
, still doesn't work.
Any idea ?
Upvotes: 1
Views: 4399
Reputation: 41393
The Style triggers have a higher priority than the ControlTemplate triggers, as discussed here.
What you can do is "target" the element that uses the Background brush and set it directly. So you probably have something like the following in your ControlTemplate:
<Border Background="{TemplateBinding Background}" ... >
You can change this to:
<Border x:Name="border" Background="{TemplateBinding Background}" ... >
Then change your ControlTemplate DataTrigger to:
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsCritical}" Value="True">
<Setter TargetName="border" Property="Background" Value="Red" />
</DataTrigger>
</ControlTemplate.Triggers>
This moves the ControlTemplate Trigger's setter's priority up ahead from the Style triggers.
Upvotes: 2