加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从DataGrid中选择DataGridCell

发布时间:2020-12-15 17:47:11 所属栏目:百科 来源:网络整理
导读:我有一个DataGrid WPF控件,我想得到一个特定的DataGridCell.我知道行和列索引.我该怎么做? 我需要DataGridCell,因为我必须访问它的内容.所以如果我有(例如)一列DataGridTextColum,我的内容将是一个TextBlock对象. 解决方法 您可以使用与此类似的代码来选择
我有一个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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读