[英]how to get selected column name or index in WPF Grid
您能告訴我如何在WPF網格中獲取選定的列名或索引嗎?
對於DataGrid,可以通過CurrentCell屬性獲得該列:
DataGridCellInfo cellInfo = dataGrid.CurrentCell;
DataGridColumn column=cellInfo.Column;
這就是我們如何獲取特定單元格的值
Object obj = GetCell(3).Content;
string cellContent = String.Empty;
if (obj != null)
{
if (obj is TextBox)
cellContent = ((TextBox)(obj)).Text;
else
cellContent = ((TextBlock)(obj)).Text;
}
private DataGridCell GetCell(int column)
{
DataGridRow rowContainer = GetRow();
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(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.
customDataGrid.UCdataGridView.ScrollIntoView(rowContainer, customDataGrid.UCdataGridView.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.