datagrid wpf 获取选中_c# WPF DataGrid 获取选中单元格信息

private void Button_Click(objectsender, RoutedEventArgs e)

{

DataGridCell cell= dg.GetCell(1, 2);

TextBlock tb= cell.Content asTextBlock;

Console.WriteLine(tb.Text);

}public static classDataGridExtension

{///

///获取DataGrid控件单元格///

/// DataGrid控件

/// 单元格所在的行号

/// 单元格所在的列号

/// 指定的单元格

public static DataGridCell GetCell(this DataGrid dataGrid, int rowIndex, intcolumnIndex)

{

DataGridRow rowContainer=dataGrid.GetRow(rowIndex);if (rowContainer != null)

{

DataGridCellsPresenter presenter= GetVisualChild(rowContainer);

DataGridCell cell=(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);if (cell == null)

{

dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]);

cell=(DataG

你可能感兴趣的:(datagrid,wpf,获取选中)