c# - WPF DataGrid 行的颜色多绑定(也)拥有 IsSelected 属性?
问题描述
我有一个 DataGrid,其中 ItemsSource 连接到 SQL 表。我需要对行背景进行颜色编码。所以我为 DataRow 设置了一个样式:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource rowColor}">
// here I'm passing DataRowView that gives me access to row data - this is working well
<Binding />
// below part is not working (compiling, but passing nothing)
<Binding Source="{RelativeSource Mode=Self}" Path="IsSelected" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
事情是我需要根据行的数据设置许多颜色。即绿色、黄色、红色......这是为了立即视觉识别每个项目的状态。这部分工作完美。
但是我还需要对每一行进行颜色编码,当它处于选定状态时。否则,一旦选择,它们都将具有无用的默认 RoyalBlue 背景。所以对于选定的行,我需要设置 DarkGreen、DarkYellow、DarkRed... 等。因此,MultiBinding 和我尝试将自己的 IsSelected 属性传递给转换器。那是行不通的。无论我尝试了什么(我尝试了很多,还有其他属性),在我的 Convert() 函数中,values[0] 都可以(DataRowView),但 values[1] 始终是一个 DependencyObject.Unset。
这样做的正确方法是什么?
解决方案
尝试设置RelativeSource
属性而不是Source
:
<Binding RelativeSource="{RelativeSource Self}" Path="IsSelected" />
推荐阅读
- django - Django限制在外键中的一个选择
- firebase - 在 initState() 中使用异步方法颤动
- mysql - 使用 mysql npm 库插入行时出现错误
- python - 基于重复拆分python列表
- ruby-on-rails - 如何在 i18n 中翻译条件语句
- java - SQL 查询在 Java 中失败,但在外部有效
- c# - 将字符串绑定到模型属性时配置 DateTime 格式
- asp.net-core - 如何在.Net中相同服务的不同实例之间进行通信?
- php - Symfony Messenger 如何确定应由哪个处理程序处理每种类型的消息?
- android - 当我将对象从片段发送到另一个片段时,原始数据已更新