【问题标题】:How to add a new line on TAB key in WPF dataGrid如何在 WPF dataGrid 中的 TAB 键上添加新行
【发布时间】:2011-05-04 09:23:57
【问题描述】:

当我在数据网格的最后一个单元格上按“TAB”键时,我想在我的数据网格中添加一个新行。

我正在使用 MVVM 模式来执行此操作。我有一个解决方案,我将 Tab 键分配给数据网格的输入绑定:

    <DataGrid.InputBindings>
       <KeyBinding Command="{Binding Path=InsertNewLineCommand}" Key="Tab"></KeyBinding>
   </DataGrid.InputBindings>

并在 InsertNewLineCommand 中添加以下代码:

private void ExecuteInsertNewLineCommand()
    {
        //Checked is SelectedCell[0] at last cell of the datagrid
        {
            InsertNewLine();
        }
    }

但问题在于在网格禁用上添加 KEYBINDING='TAB' 我的正常选项卡功能(移动到下一个单元格等等......)

【问题讨论】:

  • 您似乎已经在检查 SelectedCell 是否是最后一个,为什么不在之后添加一个 else,然后以编程方式将 SelectedCell 移动到 DataGrid 中的下一个单元格?

标签: wpf datagrid insert key


【解决方案1】:

只需确定您是否在最后一列,然后执行您的命令。

我正在使用 PreviewKeyDown,所以我可以测试逻辑,但您可以将它放在您的 executeCommand 方法中。无论如何,这应该让你开始:

<DataGrid PreviewKeyDown="DataGrid_PreviewKeyDown" SelectionUnit="Cell" ....

private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (!Keyboard.IsKeyDown(Key.Tab)) return;
    var dataGrid = (DataGrid) sender;

    var current = dataGrid.Columns.IndexOf(dataGrid.CurrentColumn);
    var last = dataGrid.Columns.Count - 1;

    if (current == last)
         ExecuteInsertNewLineCommand();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    相关资源
    最近更新 更多