DataGridRow 背景颜色的 DataTrigger
如果我的项目很重要,我尝试绑定 DataGridRow 触发器将背景设置为红色。否则,我想要 AlternatingRowBackground
。这是我到目前为止所得到的:
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsCritical}" Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</ControlTemplate.Triggers>
它工作正常,但它禁用了AlternatingRowBackground(我在某处读到这是正常的)。所以我想手动执行此操作并自己设置行背景:
<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>
但这样做它永远不会变成红色(关键绑定触发器)。我已尝试 MultiDataTrigger
并具有两个条件(IsCritical -> False && Alternate),但绑定失败。我还尝试将两个触发器放入 Style
和 ControlTemplate
中,但仍然不起作用。
有什么想法吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
样式触发器的优先级高于 ControlTemplate 触发器,如此处所述。
您可以做的是“定位”使用背景画笔的元素并直接设置它。因此,您的 ControlTemplate 中可能有类似以下内容:
您可以将其更改为:
然后将您的 ControlTemplate DataTrigger 更改为:
这会将 ControlTemplate 触发器的设置器的优先级移至样式触发器之前。
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:
You can change this to:
Then change your ControlTemplate DataTrigger to:
This moves the ControlTemplate Trigger's setter's priority up ahead from the Style triggers.