예제 #1
0
        private static void OnScrollToView(object sender, SelectedCellsChangedEventArgs e)
        {
            if (sender is DataGrid)
            {
                DataGrid d = (DataGrid)sender;
                if (d.SelectedItem != null)
                {
                    d.UpdateLayout();
                    d.ScrollIntoView(d.SelectedItem);
                    if (GetSetKeyboardFocusOnScrollToView(d))
                    {
                        #region settign keyboard focus to a first cell on the row
                        //TODO check if it is a normal solution
                        DataGridRow row = (DataGridRow)d.ItemContainerGenerator.ContainerFromIndex(d.SelectedIndex);
                        if (row != null)
                        {
                            DataGridCellsPresenter dgCP = row.GetVisualChildren <DataGridCellsPresenter>().FirstOrDefault();

                            if (dgCP == null)
                            {
                                d.ScrollIntoView(row, d.Columns[0]);
                                dgCP = row.GetVisualChildren <DataGridCellsPresenter>().FirstOrDefault();
                            }

                            if (dgCP != null)
                            {
                                DataGridCell cell = (DataGridCell)dgCP.ItemContainerGenerator.ContainerFromIndex(0);
                                if (cell != null)
                                {
                                    cell.Focus();
                                }
                            }
                        }
                        #endregion                         // settign keyboard focus to a first cell on the row
                    }
                }
            }
        }