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

时间:2015-07-02 12:17:52

标签: wpf datagrid newline edit commit

DataGrid存在问题。有时(通常每隔3/4 ...次)用[enter]键提交值将开始编辑下一个单元格,并自动键入换行符char(使单元格为2行),替换当前值。就像添加[enter]键一样,用户可以输入密钥。

我进行了调试,当按下Enter时会发生什么,使用Commit调用CellEditEnding(),但是在#34;某人"调用BeginningEdit()之后。这个不必要的BeginningEdit()的callstack与我开始手动编辑单元格的相同。 再次注意,这只是偶尔发生(但可重复)。 也许有人有一个想法,我应该从这个问题开始做什么? 重要的是,只有在WPF控件通过ElementHost嵌入到.NET控件中并且它被用作COM控件(在我们的例子中,来自C ++代码)时才会发生。 如果直接在WPF窗口中使用WPF控件,那很好,没有这样的行为。

我附上一个屏幕以便更好地理解。

enter image description here

1 个答案:

答案 0 :(得分:0)

我想我找到了解决方案,在这里得到了一些提示:WPF TextBox not accepting Input when in ElementHost in Window Forms

问题似乎是将WPF控件集成到.NET Forms中。 按Enter键时,将发送以下事件: WM_KEYDOWN + WM_CHAR + WM_KEYUP。

忽略输入的WM_CHAR似乎解决了双重问题,但保持输入键正常工作。

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));
};