title: Context menu does not respond to key press when opened from WindowsFormsHost
body: <p>When focus is in a WindowsFormsHost and a context menu opens, the context menu does not respond to up/down arrow key presses.</p>
  
  <p>The problem is illustrated by the following (almost self contained) code:</p>
  
  <pre><code>&lt;Window x:Class="FocusAndWinformsSimple.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
          Title="MainWindow" Height="350" Width="525"&gt;
    &lt;DockPanel Name="DockPanel"&gt;
      &lt;DockPanel.ContextMenu&gt;
        &lt;ContextMenu&gt;
          &lt;MenuItem Header="aaa"/&gt;
          &lt;MenuItem Header="bbb"/&gt;
        &lt;/ContextMenu&gt;
      &lt;/DockPanel.ContextMenu&gt;
      &lt;StackPanel&gt;
        &lt;TextBox/&gt;
        &lt;WindowsFormsHost&gt;
          &lt;wf:MaskedTextBox/&gt;
        &lt;/WindowsFormsHost&gt;
      &lt;/StackPanel&gt;
    &lt;/DockPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>and code behind:</p>
  
  <pre><code>  public partial class MainWindow : Window
    {
      public MainWindow()
      {
        InitializeComponent();
        InputBindings.Add(
          new KeyBinding(
            new RelayCommand(OpenContextMenu),
            new KeyGesture(Key.T, ModifierKeys.Control)));
      }
  
      void OpenContextMenu()
      {
        DockPanel.ContextMenu.PlacementTarget = DockPanel;
        DockPanel.ContextMenu.Placement = PlacementMode.Center;
        DockPanel.ContextMenu.IsOpen = true;
      }
    }
  </code></pre>
  
  <p>This will display two text boxes, a WPF and a WinForms. If you press Ctrl-T a simple context menu will appear.</p>
  
  <p>Now run the program</p>
  
  <ol>
  <li>Place the cursor in the WPF text box</li>
  <li>Press Ctrl-T: the context menu appears</li>
  <li>Press down-arrow: The first item in the context menu ('aaa') is selected.</li>
  </ol>
  
  <p>whereas</p>
  
  <ol>
  <li>Place the cursor in the WinForms text box</li>
  <li>Press Ctrl-T: the context menu appears</li>
  <li>Press down-arrow: Nothing happens</li>
  </ol>
  
  <p>In our application, keyboard navigation is essential, so this is very annoying.</p>
  
  <p>Can anybody help me explain? And preferably come up with a solution, making the second case work as the first.</p>
  
  <p>(I have noticed, that normal menus - at least opened using mnemonics - work fine).</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I guess, I should elaborate a bit on the real case, I'm working on: I have a TabControl where the content of each TabItem is developed elsewhere (but typically complex). My Ctrl-T context menu will show a list of the tab-item titles for quick navigation between tabs. (It mimicks the combobox to the right of tabs in Visual Studio).</p>
  
  <p>Now, when the content of the present tab is a WPF control and focus is within the control, everything works fine. If you open the context menu and choose the same tab, focus will return to where it was before the context menu opened. This works since the context menu is a focus scope, so logical focus remains where it was within the control.</p>
  
  <p>However, if the content is a WindowsFormHost, and focus is within, the context menu does not work as explained above. The first answer below fixes this by moving focus to any WPF control before opening the context menu. Unfortunately, this also changes logical focus making it impossible to reset it after the context menu closes.</p>
  
  <p>I have tried to capture and save focus before changing focus and opening the context menu, with no luck.</p>
  
  <p>The solution I have settled for is to change focus only when necessary. (checkin for <code>Keyboard.FocusedElement == null</code> seems to work). This means that my solution is only sub-optimal in the case where you have focus in a WinForms component, open the context menu and choose not to change tab. This is rare, so it is a great improvement over my first problem.</p>
  
  <p>But I'm still curious to see whether a better solution exists.</p>