Note that there are some explanatory texts on larger screens.

plurals
  1. POKeyboard-Problems with WPF UserControl inside WinForms
    primarykey
    data
    text
    <p>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. </p> <p>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 <em>Keyboard.Modifiers</em> explicitly to determine if the Ctrl-Key is pressed, <em>Keyboard.Modifiers</em> 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 <em>Integration.ElementHost.EnableModelessKeyboardInterop(window)</em> but I do not have a WPF-Window, I just have a WPF-UserControl. </p> <p>I created a new simplified project to reduced complexity to a minimum.</p> <p><strong>XAML</strong></p> <pre><code>&lt;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"&gt; &lt;TextBlock Text="This is a Demo-Text"&gt; &lt;TextBlock.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Header="First" Click="MenuItem_Click"&gt;&lt;/MenuItem&gt; &lt;MenuItem Header="Second"&gt;&lt;/MenuItem&gt; &lt;/ContextMenu&gt; &lt;/TextBlock.ContextMenu&gt; &lt;/TextBlock&gt; &lt;/UserControl&gt; </code></pre> <p><strong>Code behind</strong></p> <pre><code>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") ' &lt;= I am always here, even if I press Ctrl Else Debug.WriteLine("Modifiers = Not None") End If End Sub End Class </code></pre> <p>I put this Control into an <em>ElementHost</em> 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, <em>Keyboard.Modifiers</em> still returns <em>Modifiers.None</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload