c# – 从DataGrid中选择DataGridCell

我有一个DataGrid WPF控件,我想得到一个特定的DataGridCell.我知道行和列索引.我该怎么做?

我需要DataGridCell,因为我必须访问它的内容.所以如果我有(例如)一列DataGridTextColum,我的内容将是一个TextBlock对象.

解决方法

您可以使用与此类似的代码来选择一个单元格:
var dataGridCellInfo = new DataGridCellInfo(
    dataGrid.Items[rowNo],dataGrid.Columns[colNo]);

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;

我看不到直接更新特定单元格的内容方法,所以为了更新特定单元格的内容,我将执行以下操作

// gets the data item bound to the row that contains the current cell
// and casts to your data type.
var item = dataGrid.CurrentItem as MyDataItem;

if(item != null){
    // update the property on your item associated with column 'n'
    item.MyProperty = "new value";
}
// assuming your data item implements INotifyPropertyChanged the cell will be updated.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...