WPF datagrid 'newitemplaceholderposition' is not allowed during a transaction begun by 'addnew'

前端 未结 5 803
夕颜
夕颜 2021-01-02 23:49

I have a tabControl on a WPF form.

In one of the Tab Items I have a User Control that contains a DataGrid which has CanUserAddRows="True". The

5条回答
  •  感情败类
    2021-01-03 00:10

    I just ran into the same problem. Found two possible workarounds:

    1/ Trigger the CommitEdit event of the DataGrid, then call CommitEdit. I'm not sure why this last step is needed, you may not have to call CommitEdit in your case.

            DataGrid.CommitEditCommand.Execute(this.DataGridWorkItems, this.DataGridWorkItems);
    
            yourDataGrid.CommitEdit(DataGridEditingUnit.Row, false);
    

    2/ Simulate a stroke on the 'Return' key of the keyboard:

            var keyEventArgs = new KeyEventArgs(InputManager.Current.PrimaryKeyboardDevice,PresentationSource.FromDependencyObject(yourDataGrid), System.Environment.ProcessorCount, Key.Return);
            keyEventArgs.RoutedEvent = UIElement.KeyDownEvent;
            yourDataGrid.RaiseEvent(keyEventArgs);
    

    I settled for the last solution, since I had a few fishy side effects with the first one.

提交回复
热议问题