Datagrid: Как получить CurrentCell SelectedItem? - PullRequest
5 голосов
/ 12 мая 2011

В коде сетки данных WPF как получить currentCell из моего dataGrid.SelectedItem (в коде)?

Большое спасибо,

Ответы [ 2 ]

8 голосов
/ 12 мая 2011

Попробуйте это из post

Вы можете извлечь строку из dataGrid.SelectedIndex и столбец с помощью dataGrid.CurrentColumn.DisplayIndex

public static DataGridCell GetCell(DataGrid dataGrid, int row, int column)
        {
            DataGridRow rowContainer = GetRow(dataGrid, row);
            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
                    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;
    }

Вы можете найти полный исходный код здесь (ищите код в конце страницы)

0 голосов
/ 12 мая 2011

Вы можете использовать свойство CurrentCell.Item в самой DataGrid:

DataGridCell cell = (DataGridCell)myDataGrid.CurrentCell.Item;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...