0

There is an issue with the DataGrid. Sometimes (usually every 3rd/4th... times) committing the value with [enter] key will start editing the next cell, and automatically types a newline char (making the cell 2 lines), replacing the current value. So like [enter] key is added as if user would have typed it.

I debugged, and what happens when Enter is pressed, CellEditEnding() called with Commit, but right after BeginningEdit() is called by "somebody". The callstack for this unnecessary BeginningEdit() is the same as I start to edit the cell manually. Note again, this only happens sporadically (but reproducible). Maybe someone has an idea, what should I start to do with this issue? Important, it only happens, if the WPF control is embedded into a .NET control via ElementHost, and it's used as a COM control (in our case,from a C++ code). If the WPF control is used directly in a WPF Window, it's fine, no such behavior.

I attach a screen for better understanding.

enter image description here

Zoli
  • 841
  • 8
  • 31

1 Answers1

0

I think I found the solution, got some hints here: WPF TextBox not accepting Input when in ElementHost in Window Forms

The problem seems to be with the integration of the WPF control into .NET Forms. When enter is pressed the following events are sent: WM_KEYDOWN + WM_CHAR + WM_KEYUP.

Ignoring the WM_CHAR for Enter seems to solve the double issue, but keep the enter key working.

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));
};
Community
  • 1
  • 1
Zoli
  • 841
  • 8
  • 31