从 DataGrid 中选择 DataGridCell [英] Select DataGridCell from DataGrid

查看:33
本文介绍了从 DataGrid 中选择 DataGridCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

你可以使用类似这样的代码来选择一个单元格:

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.

这篇关于从 DataGrid 中选择 DataGridCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆