![](/img/trans.png)
[英]Change background of WPF DataGrid cell depending on value when populated with a datatable as IListSource
[英]Change Background of Datagrid row depending on datatable Value
我正在努力解决一个看似简单的问题:我有一个绑定到数据表的数据网格。 该数据表包含一个名为“ COLORSTATUS”(枚举值)的列-我想根据ColorStatus绘制数据表的每个ROW。 我试图建立一个值转换器-但我无法将其传递给整行和/或数据表。 我已经想过将Caligris Messages与死死的DataGridRow事件挂钩-但是如何在XAML中执行此操作-我只能访问DataGrid.RowStyle Elements。
<DataGrid x:Name="excelDataTable_ExcelData" cal:Message.Attach="[Event AutoGeneratedColumns] = [Action HideTheColorColumn($source)]">
?? What to do here
</DataGrid>
我决定采用以下解决方案:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding Row, Converter={StaticResource ExcelRowColorConverter}}"></Setter>
</Style>
</DataGrid.RowStyle>
我有点惊讶,您可以通过“行”。 作为转换器的提示:传递的对象是实际的DataRow。
转换器应该可以正常工作,但是您必须确保将其应用在正确的位置。
只需将其传递给DataRow
,从中获取“ COLORSTATUS”列值,然后返回适当的颜色笔刷即可。
例如,
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow }">
<!-- DataContext will be your DataRow -->
<Setter Property="Background"
Value="{Binding Converter={StaticResource MyColorConverter}}" />
</Style>
</DataGrid.Resources>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.