我有一个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.