как получить выбранное имя столбца или индекс в WPF Grid - PullRequest
1 голос
/ 08 августа 2011

Подскажите, пожалуйста, как получить название выбранного столбца или индекс в WPF Grid.

Ответы [ 3 ]

1 голос
/ 08 августа 2011

Для DataGrid столбец, который вы можете получить через свойство CurrentCell:

DataGridCellInfo cellInfo = dataGrid.CurrentCell;
DataGridColumn column=cellInfo.Column;
0 голосов
/ 24 августа 2011

Вот как мы можем получить значение определенной ячейки

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;
    }
0 голосов
/ 08 августа 2011

Попробуйте получить список выбранных строк:

IList rows = dg.SelectedItems;

С этот связанный вопрос .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...