Datagrid:如何获取SelectedItem的CurrentCell?
在WPF datagrid的代码背后,如何从我的dataGrid.SelectedItem(In Code)中获取currentCell?
非常感谢,
从post中试试这个
您可以通过dataGrid.CurrentColumn.DisplayIndex
从dataGrid.SelectedIndex
和column中检索行。
public static DataGridCell GetCell(DataGrid dataGrid, int row, int column) { DataGridRow rowContainer = GetRow(dataGrid, row); if (rowContainer != null) { DataGridCellsPresenter presenter = GetVisualChild(rowContainer); // try to get the cell but it may possibly be virtualized DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); if (cell == null) { // now try to bring into view and retreive the cell dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]); cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); } return cell; } return null; }
编辑
public static DataGridRow GetRow(DataGrid dataGrid, int index) { DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index); if (row == null) { dataGrid.ScrollIntoView(dataGrid.Items[index]); dataGrid.UpdateLayout(); row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index); } return row; }
你可以在这里找到完整的源代码(在页面末尾查找代码)
您可以在DataGrid本身中使用CurrentCell.Item属性:
上述就是C#学习教程:Datagrid:如何获取SelectedItem的CurrentCell?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
DataGridCell cell = (DataGridCell)myDataGrid.CurrentCell.Item;
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1005175.html