二、WPF datagrid 特定行变色
public void Color()
{
DataGridRow row1 = (DataGridRow)this.dgSource.ItemContainerGenerator.ContainerFromIndex(i); if (row1 != null) row1.Background = new SolidColorBrush(Colors.Red);
}
这里有个坑,初始化时不能变色,必须等datagrid完成渲染后才能进行这种行变色。
因而先把form show 出来之后才执行变色方法。这个方法因为莫名其妙的BUG会导致变色不是我们想要的效果!!
CompareForm cf = new CompareForm(); cf.Show(); cf.Color();
第二种方法。用datagrid 的 Dg_LoadingRow方法
private void Dg_LoadingRow(object sender, DataGridRowEventArgs e) { DataGridRow row = e.Row; OrgTypeAuthResponseModel dataRow = e.Row.Item as OrgTypeAuthResponseModel; if (dataRow == null) return; if (dataRow.Level == 1)//新字段紫色 { if (row != null) row.Background = new SolidColorBrush(Colors.LightBlue); } else if (dataRow.Level == 2) { if (row != null) row.Background = new SolidColorBrush(Colors.LightCyan); } else { if (row != null) row.Background = new SolidColorBrush(Colors.White); } }