简体   繁体   English

更改DataGrid WPF中特定行的背景

[英]Change background for specific rows in datagrid wpf

I have a datagrid "Funds" bind to a listcollectionview. 我有一个数据网格“资金”绑定到listcollectionview。

I want to highlight specific rows in datagrid based on few rules. 我想根据一些规则突出显示数据网格中的特定行。 The fundGroupsList is the list of group names. fundGroupsList是组名称的列表。 The rule1 is to find the misalignment within members of a group. 规则1是查找组内成员之间的错位。

One of the ways to do that is to create a boolean property (ex. IsRule1Failed ) in Entries and save the fail result foreach row . 做到这一点的方法之一是在Entries中创建一个布尔属性 (例如IsRule1Failed ),然后为每个行保存失败结果

Then, in XAML use DataTriggers to change the background color. 然后,在XAML中,使用DataTriggers更改背景颜色。

<DataGrid.Resources>
      <Style TargetType="DataGridCell">
          <Style.Triggers>
             <Trigger Property="IsSelected" Value="True">
                 <Setter Property="Background" Value="BurlyWood" />
             </Trigger>
             <DataTrigger Binding="{Binding DataContext.IsRule1Failed}" Value="True">
                 <Setter Property="Background" Value="Red"></Setter>
             </DataTrigger>
          </Style.Triggers>
      </Style>
 </DataGrid.Resources>

You can also simply change the CSS Class of the row. 您也可以简单地更改该行的CSS类。 GridView.SelectedRowStyle.CssClass = "selectedrow"; GridView.SelectedRowStyle.CssClass =“ selectedrow”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM