【问题标题】:WPF: How to change the background color of a datagrid cell on runtimeWPF:如何在运行时更改数据网格单元格的背景颜色
【发布时间】:2012-10-24 07:17:59
【问题描述】:

我在两个数据网格中显示两个数据表(我们称它们为左右),它可以工作。但是我想要做的是允许用户根据选择比较两行(左和右),并在单元格不同时更改背景。

示例:


|A|B|C|
|1|2|3|
|1|2|3|
|1|2|3|


|A|B|C|
|1|2|4|
|1|2|3|
|1|2|3|

在 XAML 中,我的数据网格看起来像:<DataGrid Grid.Column="0" x:Name="leftData" HorizontalAlignment="Stretch" > </DataGrid>

在代码中,我将数据网格绑定到 DataTable:

TableRows = new DataTable();
leftData.ItemsSource = TableRows;

当用户选择左右第一行时,C列的单元格应该用红色背景标记。

在 WPF 中执行此操作的更好方法是什么?是否可以在 WPF 中使用 DataGrid 来做到这一点?

【问题讨论】:

    标签: wpf datagrid datatable


    【解决方案1】:

    我终于找到了解决方案。 DataGrid 控件不提供获取 DataGridCell 的方法,但是可以使用 VisualTreeHelper 从 DataGrid 控件获取它,以便获取 DataGridCellsPresenter,并且可以从演示者获取 DataGridCell。

    更多信息和代码可以在这里找到:

    http://techiethings.blogspot.ch/2010/05/get-wpf-datagrid-row-and-cell.html

    【讨论】:

      【解决方案2】:

      你可以做类似的事情

      //dataControl would be the name of your control in the XAML
      var cell = dataControl.Cells[columnIndex,rowIndex];
      
      cell.Background = new SolidBrush(Colours.Red);
      

      显然,您需要在您当前拥有的任何逻辑中实现这一点,如果您可以发布更多内容,那么我会更好地了解它的去向

      【讨论】:

      • 嗨 Steoates,我编辑了这个问题。问题是 DataGrid 没有您在代码中提到的任何属性单元格。
      猜你喜欢
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 2021-01-04
      相关资源
      最近更新 更多