static Microsoft.Windows.Controls.DataGridCell TryToFindGridCell(Microsoft.Windows.Controls.DataGrid grid, Microsoft.Windows.Controls.DataGridCellInfo cellInfo)
 {
     Microsoft.Windows.Controls.DataGridCell result = null;
     Microsoft.Windows.Controls.DataGridRow  row    = null;
     try
     {
         grid.ScrollIntoView(cellInfo.Item);
         grid.UpdateLayout();
         row = (Microsoft.Windows.Controls.DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);
         if (row != null)
         {
             int columnIndex = grid.Columns.IndexOf(cellInfo.Column);
             if (columnIndex > -1)
             {
                 Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter presenter = GetVisualChild <Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter>(row);
                 result = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as Microsoft.Windows.Controls.DataGridCell;
             }
         }
     }
     catch (Exception generalException)
     {
         string error = generalException.Message;
         result = null;
     }
     return(result);
 }
Пример #2
0
        private void ToolkitDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            Microsoft.Windows.Controls.DataGrid dataGrid = sender as Microsoft.Windows.Controls.DataGrid;
            if (dataGrid == null)
            {
                return;
            }

            if (e.Key == Key.Enter || e.Key == Key.Tab || e.Key == Key.Down || e.Key == Key.Up || e.Key == Key.Right || e.Key == Key.Left)
            {
                if (dataGrid.ItemsSource != null && dataGrid.SelectedIndex == dataGrid.Items.Count - 2)
                {
                    var border = VisualTreeHelper.GetChild(dataGrid, 0) as Decorator;
                    if (border != null)
                    {
                        var scroll = border.Child as ScrollViewer;
                        if (scroll != null)
                        {
                            scroll.ScrollToEnd();
                        }
                    }
                }
            }


            if (e.Key == Key.Enter || e.Key == Key.Tab)
            {
                TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
                request.Wrapped = true;

                Microsoft.Windows.Controls.DataGridRow rowContainer = (Microsoft.Windows.Controls.DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.CurrentItem);
                if (rowContainer != null)
                {
                    int columnIndex = dataGrid.Columns.IndexOf(dataGrid.CurrentColumn);
                    Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter presenter = GetVisualChild <Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
                    dataGrid.CommitEdit();
                    Microsoft.Windows.Controls.DataGridCell cell = (Microsoft.Windows.Controls.DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                    cell.MoveFocus(request);
                    dataGrid.SelectedItem = dataGrid.CurrentItem;
                    e.Handled             = true;
                    dataGrid.UpdateLayout();
                    try
                    {
                        dataGrid.BeginEdit(e);
                    }
                    catch (System.InvalidOperationException)
                    {
                        //Ignore error if it is 'Recursive call to Automation Peer API is not valid.'
                    }
                }
            }
            else if (e.Key == Key.Down || e.Key == Key.Up || e.Key == Key.Right || e.Key == Key.Left)
            {
                int columnIndex = dataGrid.Columns.IndexOf(dataGrid.CurrentColumn);
                Microsoft.Windows.Controls.DataGridRow rowContainer = (Microsoft.Windows.Controls.DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.CurrentItem);
                if (rowContainer != null)
                {
                    Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter presenter = GetVisualChild <Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
                    Microsoft.Windows.Controls.DataGridCell currentcell = (Microsoft.Windows.Controls.DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);

                    if (currentcell.Content is ContentPresenter)
                    {
                        DatePickerCus datepicker = FindVisualChild <DatePickerCus>(currentcell);
                        if (datepicker != null && datepicker.IsDropDownOpen)
                        {
                            e.Handled = true;
                            return;
                        }

                        ComboBoxWithSearch combo = FindVisualChild <ComboBoxWithSearch>(currentcell);
                        if (combo != null && combo.IsDropDownOpen)
                        {
                            e.Handled = true;
                            return;
                        }
                    }

                    if (e.Key == Key.Down)
                    {
                        dataGrid.CommitEdit();
                        dataGrid.SelectedIndex++;

                        rowContainer = (Microsoft.Windows.Controls.DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.SelectedItem);
                        if (rowContainer != null)
                        {
                            presenter = GetVisualChild <Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
                            Microsoft.Windows.Controls.DataGridCell cell = (Microsoft.Windows.Controls.DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                            cell.Focus();
                            dataGrid.CurrentItem = dataGrid.SelectedItem;
                            e.Handled            = true;
                            dataGrid.UpdateLayout();
                            try
                            {
                                dataGrid.BeginEdit(e);
                            }
                            catch (System.InvalidOperationException)
                            {
                                //Ignore error if it is 'Recursive call to Automation Peer API is not valid.'
                            }
                        }
                    }
                    else if (e.Key == Key.Up)
                    {
                        if (dataGrid.ItemsSource != null && dataGrid.SelectedIndex == 0)
                        {
                            return;
                        }

                        dataGrid.CommitEdit();
                        dataGrid.SelectedIndex--;

                        rowContainer = (Microsoft.Windows.Controls.DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.SelectedItem);
                        if (rowContainer != null)
                        {
                            presenter = GetVisualChild <Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
                            Microsoft.Windows.Controls.DataGridCell cell = (Microsoft.Windows.Controls.DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                            cell.Focus();
                            dataGrid.CurrentItem = dataGrid.SelectedItem;
                            e.Handled            = true;
                            dataGrid.UpdateLayout();
                            try
                            {
                                dataGrid.BeginEdit(e);
                            }
                            catch (System.InvalidOperationException)
                            {
                                //Ignore error if it is 'Recursive call to Automation Peer API is not valid.'
                            }
                        }
                    }
                }
            }
        }