WPF Datagrid单元格着色

zu0ti5jz  于 2023-11-21  发布在  其他
关注(0)|答案(3)|浏览(249)

我们如何在WPF数据网格中以编程方式为单元格添加颜色?我可以为行添加颜色,但我想从后面的代码为单元格添加颜色。请提供代码?

drnojrws

drnojrws1#

这段代码的作用:首先,处理LoadingRow事件,获取行(它具有保存绑定项的Item属性),获取绑定项,进行所有需要的计算,获取需要更改的单元格,最后更改目标单元格的样式。
下面是代码(作为项目,我使用了一个带有int ID属性的示例对象,用于着色)。

C#:

  1. private void FillTheDataGrid()
  2. {
  3. List<SomeClass> list = new List<SomeClass>();
  4. Random rnd = new Random();
  5. for (int i = 0; i < 10; i++)
  6. {
  7. list.Add(new SomeClass() { DaysOld = DateTime.Now.Add(new TimeSpan(rnd.Next(5), 0, 0, 0)), ID=i});
  8. }
  9. dataGrid.ItemsSource = list;
  10. }
  11. private void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
  12. {
  13. Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => AlterRow(e)));
  14. }
  15. private void AlterRow(DataGridRowEventArgs e)
  16. {
  17. var cell = GetCell(dataGrid, e.Row, 1);
  18. if (cell == null) return;
  19. var item = e.Row.Item as SomeClass;
  20. if (item == null) return;
  21. var value = item.ID;
  22. if (value <= 1) cell.Background = Brushes.Red;
  23. else if (value <= 2) cell.Background = Brushes.Yellow;
  24. else cell.Background = Brushes.Green;
  25. }
  26. public static DataGridRow GetRow(DataGrid grid, int index)
  27. {
  28. var row = grid.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;
  29. if (row == null)
  30. {
  31. // may be virtualized, bring into view and try again
  32. grid.ScrollIntoView(grid.Items[index]);
  33. row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
  34. }
  35. return row;
  36. }
  37. public static T GetVisualChild<T>(Visual parent) where T : Visual
  38. {
  39. T child = default(T);
  40. int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
  41. for (int i = 0; i < numVisuals; i++)
  42. {
  43. var v = (Visual)VisualTreeHelper.GetChild(parent, i);
  44. child = v as T ?? GetVisualChild<T>(v);
  45. if (child != null)
  46. {
  47. break;
  48. }
  49. }
  50. return child;
  51. }
  52. public static DataGridCell GetCell(DataGrid host, DataGridRow row, int columnIndex)
  53. {
  54. if (row == null) return null;
  55. var presenter = GetVisualChild<DataGridCellsPresenter>(row);
  56. if (presenter == null) return null;
  57. // try to get the cell but it may possibly be virtualized
  58. var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
  59. if (cell == null)
  60. {
  61. // now try to bring into view and retreive the cell
  62. host.ScrollIntoView(row, host.Columns[columnIndex]);
  63. cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
  64. }
  65. return cell;
  66. }

字符串

型号:

  1. public class SomeClass
  2. {
  3. public int ID { get; set; }
  4. public DateTime DaysOld { get; set; }
  5. }

XAML:

  1. <DataGrid Name="dataGrid" AutoGenerateColumns="True" LoadingRow="dataGrid_LoadingRow"/>

展开查看全部
mm5n2pyu

mm5n2pyu2#

我也遇到了同样的问题,最后我找到了答案,它比我想的要容易,下面是代码来改变单元格的颜色:

  1. DataGridRow firstRow = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items[i]) as DataGridRow;
  2. firstColumnInFirstRow = dataGrid.Columns[j].GetCellContent(firstRow).Parent as DataGridCell;
  3. firstColumnInFirstRow.Background = Brushes.IndianRed;

字符串

nwnhqdif

nwnhqdif3#

根据DataGridCell中的内容,您可以创建绑定到颜色或使用触发器的DataTemplate

  1. <DataTemplate DataType="{x:Type cell:MyCellObject}">
  2. <TextBlock TextAlignment="Center" Text="{Binding Text}">
  3. <TextBlock.Style>
  4. <Style TargetType="TextBlock">
  5. <Setter Property="Background" Value="{Binding MyBackground}" />
  6. <Setter Property="Foreground" Value="Black" />
  7. <Setter Property="Padding" Value="0" />
  8. <Style.Triggers>
  9. <DataTrigger Binding="{Binding MyBool}" Value="True">
  10. <Setter Property="Foreground" Value="DarkRed" />
  11. <Setter Property="FontWeight" Value="Bold" />
  12. </DataTrigger>
  13. </Style.Triggers>
  14. </Style>
  15. </TextBlock.Style>
  16. </TextBlock>
  17. </DataTemplate>

字符串

展开查看全部

相关问题