简体   繁体   English

如何在WPF网格中获取选定的列名或索引

[英]how to get selected column name or index in WPF Grid

您能告诉我如何在WPF网格中获取选定的列名或索引吗?

For the DataGrid, the column you can get via the CurrentCell-property: 对于DataGrid,可以通过CurrentCell属性获得该列:

DataGridCellInfo cellInfo = dataGrid.CurrentCell;
DataGridColumn column=cellInfo.Column;

Try this to get a list of rows selected: 尝试此操作以获取所选行的列表:

IList rows = dg.SelectedItems;

From this related question . 这个相关的问题

This is how we can get the value of a specific cell 这就是我们如何获取特定单元格的值

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM