如何从wpf中的DataGridCellinfo类中提取DataGridCell

man*_*der 16 .net c# wpf datagrid

我想知道如何从DataGridCellInfo获取DataGridCell.实际上我在datagrid中有一些选定的单元格,而SelectedCells属性返回DataGridCellInfo的集合,但我想在运行时更改这些单元格的背景.所以我需要datagrid单元格.

请建议我如何操作以及如何动态地(通过代码)更改datagrid单元格背景颜色.

谢谢

Ste*_*ler 39

对于那些从搜索引擎到这里的人,希望找到问题中标题的答案,请看这里:https://stackoverflow.com/a/17066695/937093

内容:

public DataGridCell GetDataGridCell(DataGridCellInfo cellInfo)
{
    var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
    if (cellContent != null)
        return (DataGridCell) cellContent.Parent;

    return null;
}
Run Code Online (Sandbox Code Playgroud)

编辑

如果你赞成这个答案,请不要忘记提交我所链接的原始答案!


man*_*der 5

要动态更改单元格的颜色,这是最简单的方法

cell.Background = new SolidColorBrush(Colors.Green);

要获取数据网格单元格,请点击此链接

WPF Datagrid:以编程方式编辑单元格

感谢 Natxo