简体   繁体   English

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

[英]How to extract DataGridCell from DataGridCellinfo class in wpf

I want to know how to get the DataGridCell from DataGridCellInfo. 我想知道如何从DataGridCellInfo获取DataGridCell。 Actually i have a some selected cells in datagrid, and SelectedCells property return DataGridCellInfo's collection, but i want to change the background of those cells at runtime too. 实际上我在datagrid中有一些选定的单元格,而SelectedCells属性返回DataGridCellInfo的集合,但我想在运行时更改这些单元格的背景。 so i need the datagrid cell. 所以我需要datagrid单元格。

kindly suggest me how to do so and also how to change the datagrid cell background color dynamically(through code) also. 请建议我如何操作以及如何动态地(通过代码)更改datagrid单元格背景颜色。

Thanks 谢谢

To anyone who got here from a search engine, expecting to find an answer to the title in the question, look here: https://stackoverflow.com/a/17066695/937093 对于那些从搜索引擎到这里的人,希望找到问题中标题的答案,请看这里: https//stackoverflow.com/a/17066695/937093

Content: 内容:

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

    return null;
}

edit 编辑

if you upvote this answer please don't forget to upvote the original answer I linked as well! 如果你赞成这个答案,请不要忘记赞同我所链接的原始答案!

To change the color of the cell dynamically this is the simplest way 要动态更改单元格的颜色,这是最简单的方法

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

and to get the datagrid cell, follow this link 要获取datagrid单元格,请单击此链接

WPF Datagrid: Programmatically editing a cell WPF Datagrid:以编程方式编辑单元格

Thanks to Natxo 感谢Natxo

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

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