从 DataGrid 中选择 DataGridCell

编程入门 行业动态 更新时间:2024-10-24 16:30:29
本文介绍了从 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

本文发布于:2023-06-04 19:11:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/503404.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:DataGrid   DataGridCell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!