微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/csharp/96946.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐