DataGridRow 背景颜色的 DataTrigger

发布于 2024-10-25 13:53:14 字数 977 浏览 0 评论 0原文

如果我的项目很重要,我尝试绑定 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),但绑定失败。我还尝试将两个触发器放入 StyleControlTemplate 中,但仍然不起作用。

有什么想法吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜泪 2024-11-01 13:53:14

样式触发器的优先级高于 ControlTemplate 触发器,如此处所述。

您可以做的是“定位”使用背景画笔的元素并直接设置它。因此,您的 ControlTemplate 中可能有类似以下内容:

<Border Background="{TemplateBinding Background}" ... >

您可以将其更改为:

<Border x:Name="border" Background="{TemplateBinding Background}" ... >

然后将您的 ControlTemplate DataTrigger 更改为:

<ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding IsCritical}" Value="True">
        <Setter TargetName="border" Property="Background" Value="Red" />
    </DataTrigger>
</ControlTemplate.Triggers>

这会将 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:

<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文