即使在wpf datagrid中失去焦点,单元格仍保持编辑模式

时间:2022-03-22 22:17:31

I have a DataGrid in which I have two columns namely Name and Group.

我有一个DataGrid,其中有两列,即Name和Group。

I have used EnterKeyTravarsal Class, so that I can use Enter as TAB.

我使用了EnterKeyTravarsal类,因此我可以使用Enter作为TAB。

Also Based on some Conditions I add a new row to DataGrid in the ViewModel using InputBindings.

另外,根据一些条件,我使用InputBindings在ViewModel中向DataGrid添加一个新行。

But when a new row is added, I can see that the last cell that was focused just before adding a new row remains in the edit mode.

但是当添加新行时,我可以看到在添加新行之前聚焦的最后一个单元格仍处于编辑模式。

Here is a sample project that I have created to clearly explain my problem.

这是我创建的一个示例项目,用于清楚地解释我的问题。

2 个解决方案

#1


2  

Update your class EnterKeyTraversal

更新您的类EnterKeyTraversal

static void ue_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    var ue = e.OriginalSource as FrameworkElement;
    DataGrid dg = FindVisualParent<DataGrid>(ue) as DataGrid;
    DataGridCell cell = FindVisualParent<DataGridCell>(ue);

    if (e.Key == Key.Enter)
    {
        //e.Handled = true;

        if (dg != null)
        {
            dg.CommitEdit();
        }

        if (cell != null)
        {
            cell.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }
        else
        {
            ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }

        if (dg != null)
        {
            dg.BeginEdit();
        }
    }
}

#2


1  

I don't have the time to download all your code but i'll try to help.

我没有时间下载所有代码,但我会尽力帮助。

I remember i had a similar problem and i solved it using either

我记得我有类似的问题,我用其中任何一个解决了

this.grid.CommitEdit();

or

要么

this.grid.CommitEdit(DataGridEditingUnit.Row, true);

in your case you might need yo use "DataGridEditingUnit.Cell"

在您的情况下,您可能需要使用“DataGridEditingUnit.Cell”

I call the methods before inserting the new line, but I remember I had lots of problems to get them work and finally didn't get to understand them properly (they are working fine though), sorry :S

我在插入新行之前调用了这些方法,但是我记得我有很多问题要让它们正常工作,最后却没有正确理解它们(虽然它们工作得很好),对不起:S

EDIT: Code por public DataGrid

编辑:代码公共DataGrid

public DataGrid MyGrid
{
  get 
  {
    return this.grid;
  }
}

#1


2  

Update your class EnterKeyTraversal

更新您的类EnterKeyTraversal

static void ue_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    var ue = e.OriginalSource as FrameworkElement;
    DataGrid dg = FindVisualParent<DataGrid>(ue) as DataGrid;
    DataGridCell cell = FindVisualParent<DataGridCell>(ue);

    if (e.Key == Key.Enter)
    {
        //e.Handled = true;

        if (dg != null)
        {
            dg.CommitEdit();
        }

        if (cell != null)
        {
            cell.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }
        else
        {
            ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }

        if (dg != null)
        {
            dg.BeginEdit();
        }
    }
}

#2


1  

I don't have the time to download all your code but i'll try to help.

我没有时间下载所有代码,但我会尽力帮助。

I remember i had a similar problem and i solved it using either

我记得我有类似的问题,我用其中任何一个解决了

this.grid.CommitEdit();

or

要么

this.grid.CommitEdit(DataGridEditingUnit.Row, true);

in your case you might need yo use "DataGridEditingUnit.Cell"

在您的情况下,您可能需要使用“DataGridEditingUnit.Cell”

I call the methods before inserting the new line, but I remember I had lots of problems to get them work and finally didn't get to understand them properly (they are working fine though), sorry :S

我在插入新行之前调用了这些方法,但是我记得我有很多问题要让它们正常工作,最后却没有正确理解它们(虽然它们工作得很好),对不起:S

EDIT: Code por public DataGrid

编辑:代码公共DataGrid

public DataGrid MyGrid
{
  get 
  {
    return this.grid;
  }
}