WPF DataGridRow - 考虑触发器中的多个条件
使用 WPF 数据网格时,我需要遵守以下要求:
- IsMouseOver 时更改行背景。
- 当满足关键属性时,将行背景更改为红色。
- 当选择一行时,将行背景更改为紫色,但并不重要。
- 当行被选中且处于关键状态时,将行背景更改为深红色。
到目前为止我还不能满足最后一个条件。我现在的代码是:
<Style x:Key="GridRow" TargetType="DataGridRow">
<Setter Property="FontSize" Value="10" />
<Setter Property="Foreground" Value="#000000" />
<Setter Property="Background" Value="#E5E5E5" />
<Setter Property="Height" Value="24" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#F5F5F5" />
</Trigger>
<DataTrigger Binding="{Binding IsStatusCritical}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="#660066" />
</Trigger>
</Style.Triggers>
</Style>
Working with a WPF datagrid, I need to comply with these requeriments:
- Change row background when IsMouseOver.
- Change row background to red when a critical property is met.
- Change row background to violet when a row is selected, but not critical.
- Change row background to dark red when a row is selected and critical.
I cannot met the last condition so far. My code right now is:
<Style x:Key="GridRow" TargetType="DataGridRow">
<Setter Property="FontSize" Value="10" />
<Setter Property="Foreground" Value="#000000" />
<Setter Property="Background" Value="#E5E5E5" />
<Setter Property="Height" Value="24" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#F5F5F5" />
</Trigger>
<DataTrigger Binding="{Binding IsStatusCritical}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="#660066" />
</Trigger>
</Style.Triggers>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 MultiDataTriggers 解决此问题。只要确保按正确的顺序放置它们即可,我记得,满足所有条件的最后一个触发器优先。
You can solve this using MultiDataTriggers. Just make sure that you place them in the correct order, as I recall, the last trigger that meets all criteria takes precedence.