15

我想要一个自定义 DataGrid,它可以,

  1. 按下键时移动到下一个单元格,Enter如果它处于编辑模式。
  2. 当到达当前行的最后一列时,焦点应移动到下一行的第一个单元格。
  3. 到达下一个单元格时,如果该单元格是可编辑的,它应该自动变为可编辑的。
  4. 如果单元格包含ComboBox非组合框列,则组合框应 DropDownOpen。

请帮助我。从过去的几天开始,我一直在尝试通过创建自定义 DataGrid 并在

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

但我失败了。

4

6 回答 6

13

一个更简单的实现。这个想法是捕获 keydown 事件,如果键是“Enter”,则移动到下一个选项卡,即网格的下一个单元格。

/// <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));
    }
}
于 2014-03-11T17:20:00.563 回答
6
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;
}
于 2012-05-04T04:40:18.787 回答
6

这个解决方案怎么样?Enter通过设置取消键的动作,Handled=trueTab键。

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);
  }
}
于 2019-06-14T01:11:44.433 回答
2
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();
            }
        }
    }
}

暂时,我已经写了这个并且它为我工作。

于 2012-05-11T05:56:41.840 回答
0
    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
于 2018-08-29T08:09:55.163 回答
0

关键方法是“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 完成更新。否则会发生奇怪的事情。

例如,使用这种方案,您甚至可以在当前行后面插入一行。

于 2022-03-02T13:14:48.010 回答