2
votes

After a long and fruitless search here, this is my problem. I have a WPF UserControl that I display in a WinForms Form via an ElementHost. The development is almost done, but there are still two minor side effects.

First, if I open the ContextMenu, I expected that I can choose the items via the Cursor-Keys Up/Down. But this keypresses seem to be missing. The second is, that, if I query the Keyboard.Modifiers explicitly to determine if the Ctrl-Key is pressed, Keyboard.Modifiers always returns 'None'. I far as I read here, I assume that it has something to do with the Keyboard or Focusstates. I often read here as solution to call Integration.ElementHost.EnableModelessKeyboardInterop(window) but I do not have a WPF-Window, I just have a WPF-UserControl.

I created a new simplified project to reduced complexity to a minimum.

XAML

<UserControl x:Class="UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="50" d:DesignWidth="300">
    <TextBlock Text="This is a Demo-Text">
        <TextBlock.ContextMenu>
            <ContextMenu>
                <MenuItem Header="First" Click="MenuItem_Click"></MenuItem>
                <MenuItem Header="Second"></MenuItem>
            </ContextMenu>
        </TextBlock.ContextMenu>
    </TextBlock>
</UserControl>

Code behind

Public Class UserControl1 

  Private Sub MenuItem_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    If Keyboard.Modifiers = ModifierKeys.None Then
      Debug.WriteLine("Modifiers = None") ' <= I am always here, even if I press Ctrl
    Else
      Debug.WriteLine("Modifiers = Not None")
    End If
  End Sub
End Class

I put this Control into an ElementHost inside a WinForms-Window. If I have anything else inside the WinForms-Window (of course I have) that can capture the focus, I still can open the ContextMenu via a right click on the WPF-Control, but pressing the Cursor keys up or down does not select any of the two items. If I click the first item and hold the Ctrl-Key, Keyboard.Modifiers still returns Modifiers.None

1
Integration between WPF and WinForms is not quite water-tight. If you can't convert the WinForms app to WPF, you might be better off figuring out how to meet your needs in a WinForms control (blech).Greg Sansom
@GregSansom I try to get rid of WinForms, but it is to much for one step. I eliminated some OwnerDraw inside the replaced ListView control (choke) There is no way back ;-)Markus
I think you do have a WPF window - the context menu itself...Amittai Shapira

1 Answers

0
votes

Can someone please hit the back of my head? I was looking for a solution for this for a couple of hours, and after posting this question, the answer I found is unspectacular

Regarding the ContextMenu:

UserControl1.Focusable = True

Regarding the Keyboard.Modifiers:
I found a working alternative for Visual Basic.

If My.Computer.Keyboard.CtrlKeyDown Then ...

but I can't find the universal equivalent for the complete Framework. If someone finds some alternative, please buzz in.