从DataGrid中选择DataGridCellDataGrid、DataGridCell

由网友(谁念西风独自凉)分享简介:我有一个的DataGrid WPF控件,我希望得到一个特定的 DataGridCell 。我所知道的行和列索引。我怎样才能做到这一点?I have a DataGrid WPF control and I want to get a specific DataGridCell. I know the row an...

我有一个的DataGrid WPF控件,我希望得到一个特定的 DataGridCell 。我所知道的行和列索引。我怎样才能做到这一点?

I have a DataGrid WPF control and I want to get a specific DataGridCell. I know the row and column indices. How can I do this?

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

I need the DataGridCell because I have to have access to its Content. So if I have (for example) a column of DataGridTextColum, my Content will be a TextBlock object.

推荐答案

您可以用code与此类似,选择一个单元格:

You can use code similar to this to select a cell:

var dataGridCellInfo = new DataGridCellInfo(
    dataGrid.Items[rowNo], dataGrid.Columns[colNo]);

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

我看不到一个办法直接更新特定单元格的内容,所以为了更新特定的细胞,我会做如下的内容

I can't see a way to update the contents of a specific cell directly, so in order to update the content of a specific cell I would do the following

// 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.
阅读全文

相关推荐

最新文章