Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按下Enter键时,在WPF DataGrid中提交后调用BeginningEdit_Wpf_Datagrid_Newline_Edit_Commit - Fatal编程技术网

按下Enter键时,在WPF DataGrid中提交后调用BeginningEdit

按下Enter键时,在WPF DataGrid中提交后调用BeginningEdit,wpf,datagrid,newline,edit,commit,Wpf,Datagrid,Newline,Edit,Commit,DataGrid存在一个问题。有时(通常每3/4…次)使用[enter]键提交值将开始编辑下一个单元格,并自动键入换行符(使单元格变为2行),替换当前值。所以添加了like[enter]键,就好像用户会键入它一样 我进行了调试,当按下Enter键时会发生什么,CellEditEnding()用Commit调用,但就在BeginingEdit()之后被“someone”调用。此不必要的BeginingEdit()的调用堆栈与我开始手动编辑单元格的调用堆栈相同。 再次注意,这只会偶尔发生(但可重复)

DataGrid存在一个问题。有时(通常每3/4…次)使用[enter]键提交值将开始编辑下一个单元格,并自动键入换行符(使单元格变为2行),替换当前值。所以添加了like[enter]键,就好像用户会键入它一样

我进行了调试,当按下Enter键时会发生什么,CellEditEnding()用Commit调用,但就在BeginingEdit()之后被“someone”调用。此不必要的BeginingEdit()的调用堆栈与我开始手动编辑单元格的调用堆栈相同。 再次注意,这只会偶尔发生(但可重复)。 也许有人有一个想法,我应该如何处理这个问题? 重要的是,如果WPF控件通过EnthEntuST嵌入到.NET控件中,它被用作COM控件(在我们的例子中,来自C++代码),那么它就发生了。 如果WPF控件直接在WPF窗口中使用,则没有这种行为

为了更好地理解,我附加了一个屏幕


我想我找到了解决方案,这里有一些提示:

问题似乎在于将WPF控件集成到.NET表单中。 按下enter键时,将发送以下事件: WM_KEYDOWN+WM_CHAR+WM_keydup

忽略Enter的WM_字符似乎可以解决双重问题,但保持Enter键工作

IntPtr ChildHwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (msg == WM_CHAR)
    {
            // avoid duplicated enter when parent window is a native window 
            if (wParam.ToInt32() == 13)
                handled = true; //enter is handled by WM_KEYDOWN, and WM_CHAR follows. Removing this WM_CHAR will solve the double enter issue, but keep the enter working
    }
    if (msg == WM_GETDLGCODE)
    {
        handled = true;
        return new IntPtr(DLGC_WANTALLKEYS | DLGC_WANTARROWS | DLGC_HASSETSEL);
    }
    return IntPtr.Zero;
}
...

Loaded += delegate
{
    HwndSource s = HwndSource.FromVisual(this) as HwndSource;
    if (s != null)
        s.AddHook(new HwndSourceHook(ChildHwndSourceHook));
};