在WPF DataGrid中按Enter键时将焦点移到下一个单元格?

mqxuamgl  于 2022-11-18  发布在  其他
关注(0)|答案(7)|浏览(351)

我希望有一个自定义DataGrid,
1.在编辑模式下按Enter键时移动到下一个单元格。
1.当到达当前行的最后一列时,焦点应移动到下一行的第一个单元格。
1.在到达下一个单元格时,如果该单元格是可编辑的,则它应该自动变为可编辑的。
1.如果单元格包含一个ComboBox而不是组合框列,则该组合框应DropDownOpen。
请帮助我在这方面。我一直在尝试从过去的几天通过创建一个自定义DataGrid和写了一些代码

protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)

但我失败了。

laik7k3q

laik7k3q1#

这个键的方法是'dataGrid.SetKeyboardFocusToCell'.所以我们可以附加KeyDown事件:

private void dataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        DataGridTemplateColumn col = (DataGridTemplateColumn)dataGrid.CurrentColumn;
        if (col != null)
        {
            switch (col.SortMemberPath)
            {
                case "From":
                    if (e.Key == Key.Enter && Keyboard.Modifiers == ModifierKeys.None) // Pure Enter
                    {
                        e.Handled = true;
                        int columnIndex = dataGrid.GetColumnIndex(colTo);
                        DataGridRow currentRow = dataGrid.GetRow(dataGrid.CurrentItem);

                        dataGrid.SetKeyboardFocusToCell(dataGrid.CurrentItem, columnIndex);
                        Dispatcher.Invoke(() =>
                        {
                            GridTimeSpanBox timeSpanBox = VisualTree.FindChild<GridTimeSpanBox>(currentRow, tsb => tsb.Name == "tsbTo", true);
                            timeSpanBox.SelectAll();
                        }, System.Windows.Threading.DispatcherPriority.ContextIdle);
                    }
                    break;
            }
        } // col != null
    }

    /// <summary>
    /// Get the row container that holds the 'item'
    /// </summary>
    public DataGridRow GetRow(object item)
    {
        return (DataGridRow)ItemContainerGenerator.ContainerFromItem(item);
    }

    /// <summary>
    /// Gets the index of a 'DataGridColum' or 'DataGridTemplateColumn' in the 'Columns' list. This doesn't change if the user
    /// reorders the columns.
    /// </summary>
    public int GetColumnIndex(DataGridColumn column)
    {
        return this.Columns.IndexOf(column);
    }

在本例中,下一个框中的文本也被选中。对于大多数情况下内容被键入新内容所替换的字段,此选项非常有用。
请务必注意,一般而言,'dataGrid.SetKeyboardFocusToCell()'之后的作业必须透过发送器传送,才能让UI完成更新。否则可能会发生奇怪的事情。
例如,使用此方案,您甚至可以在当前行后面插入一行。

smdncfj3

smdncfj32#

private void dg_ExpenseItem_PreviewKeyDown(object sender, KeyEventArgs e)           
    {
        try
        {
            DataGrid grid = (DataGrid)sender;
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                var cell = GetCell(grid, grid.SelectedIndex, grid.CurrentCell.Column.DisplayIndex+1);
                if (cell != null)
                {
                    cell.IsSelected = true;
                    cell.Focus();
                    grid.BeginEdit();
                }
            }
        }
        catch (Exception ex)
        {
            
        }
    }
public static DataGridCell GetCell(DataGrid grid, int row, int column)
    {
        var rowContainer =  GetRow(grid, row);

        if (rowContainer != null)
        {
            var presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
            if (presenter != null)
            {
                // try to get the cell but it may possibly be virtualized
                var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                if (cell == null)
                {
                    // now try to bring into view and retreive the cell
                    grid.ScrollIntoView(rowContainer, grid.Columns[column]);
                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                }
                return cell;
            }
        }
        return null;
    }
static public DataGridRow GetRow(DataGrid dg, int index)
    {
        DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // may be virtualized, bring into view and try again
            dg.ScrollIntoView(dg.Items[index]);
            row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }
    static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }
gblwokeq

gblwokeq3#

一个简单得多的实现。这个想法是捕捉keydown事件,如果键是“回车”,那么移动到下一个标签,也就是网格的下一个单元格。

/// <summary>
/// On Enter Key, it tabs to into next cell.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGrid_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    var uiElement = e.OriginalSource as UIElement;
    if (e.Key == Key.Enter && uiElement != null)
    {
        e.Handled = true;
        uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    }
}
1szpjjfi

1szpjjfi4#

private void dg_PreviewKeyDown(object sender, KeyEventArgs e)
{
    try
    {
        if (e.Key == Key.Enter)
        {
            e.Handled = true;
            var cell = GetCell(dgIssuance, dgIssuance.Items.Count - 1, 2);
            if (cell != null)
            {
                cell.IsSelected = true;
                cell.Focus();
                dg.BeginEdit();
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox(ex.Message, "Error", MessageType.Error);
    }
}  

public static DataGridCell GetCell(DataGrid dg, int row, int column)
{
    var rowContainer = GetRow(dg, row);

    if (rowContainer != null)
    {
        var presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
        if (presenter != null)
        {
            // try to get the cell but it may possibly be virtualized
            var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null)
            {
                // now try to bring into view and retreive the cell
                dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }
            return cell;
        }
    }
    return null;
}
bakd9h0s

bakd9h0s5#

这个解决方案怎么样?通过设置Handled=true取消Enter键的操作,然后按Tab键。

public Constructor()
{
  InitializeComponent(); 
  this.SampleDataGrid.PreviewKeyDown += MoveCellOnEnterKey;
}

private void MoveCellOnEnterKey(object sender, KeyEventArgs e)
{
  if(e.Key == Key.Enter)
  {
    // Cancel [Enter] key event.
    e.Handled = true;
    // Press [Tab] key programatically.
    var tabKeyEvent = new KeyEventArgs(
      e.KeyboardDevice, e.InputSource, e.Timestamp, Key.Tab);
    tabKeyEvent.RoutedEvent = Keyboard.KeyDownEvent;
    InputManager.Current.ProcessInput(tabKeyEvent);
  }
}
lmyy7pcs

lmyy7pcs6#

public class DataGrid : System.Windows.Controls.DataGrid
{
    private void PressKey(Key key)
    {
        KeyEventArgs args = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, key);
        args.RoutedEvent = Keyboard.KeyDownEvent;
        InputManager.Current.ProcessInput(args);
    }
    protected override void OnCurrentCellChanged(EventArgs e)
    {
        if (this.CurrentCell.Column != null)                
            if (this.CurrentCell.Column.DisplayIndex == 2)
            {

                if (this.CurrentCell.Item.ToString() == "--End Of List--")
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
                }
            }
            else if (this.CurrentCell.Column != null && this.CurrentCell.Column.DisplayIndex == this.Columns.Count() - 1)
            {
                PressKey(Key.Return);
                DataGridCell cell = DataGridHelper.GetCell(this.CurrentCell);
                int index = DataGridHelper.GetRowIndex(cell);
                DataGridRow dgrow = (DataGridRow)this.ItemContainerGenerator.ContainerFromItem(this.Items[index]);
                dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
            }
    }
    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            DataGridRow rowContainer = (DataGridRow)this.ItemContainerGenerator.ContainerFromItem(this.CurrentItem);
            if (rowContainer != null)
            {
                int columnIndex = this.Columns.IndexOf(this.CurrentColumn);
                DataGridCellsPresenter presenter = UIHelper.GetVisualChild<DataGridCellsPresenter>(rowContainer);
                if (columnIndex == 0)
                {
                    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
                    request.Wrapped = true;
                    cell.MoveFocus(request);
                    BeginEdit();
                    PressKey(Key.Down);
                }
                else
                {
                    CommitEdit();
                    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
                    request.Wrapped = true;
                    cell.MoveFocus(request);
                }
                this.SelectedItem = this.CurrentItem;
                e.Handled = true;
                this.UpdateLayout();
            }
        }
    }
}

就目前而言,我已经写了这篇文章,它对我很有效。

zlhcx6iw

zlhcx6iw7#

Public Sub SendKey(ByVal key As Key)
     Dim args As New KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, key)
        args.RoutedEvent = Keyboard.KeyDownEvent
        InputManager.Current.ProcessInput(args)
    End Sub
Private Sub dataGrid_PreviewKeyDown(sender As Object, e As KeyEventArgs) Handles dataGrid.PreviewKeyDown
        Dim i As UIElement = e.OriginalSource
        Dim DG As DataGrid = sender
        If (e.Key = Key.Enter Or e.Key = Key.Return) AndAlso i IsNot Nothing Then
            MyBase.OnKeyDown(e)
            DG.CommitEdit()
            SendKey(Key.Tab)
            e.Handled = True
        End If
    End Sub

相关问题