title: ContextMenu on ListBox Item with DataTemplate
body: <p>I have a ListBox with different classes of items. DataTemplates are used to present those objects in the appropriate way. I want to have different context menus in the DataTemplates of these classes.</p>
  
  <p>Everything works fine using the mouse, but using the keyboard I can't bring up the context menu.</p>
  
  <p>This is probably because the keyboard-focus is not on the contents of the DataTemplate, but on the ListBoxItem. </p>
  
  <p>How can I get the ListBoxItem to refer to the Content's ContextMenu?</p>
  
  <p>Sample code:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication8.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:my="clr-namespace:WpfApplication8"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Window.Resources&gt;
      &lt;DataTemplate DataType="{x:Type my:Orange}"&gt;
          &lt;TextBlock&gt;
              Orange
              &lt;TextBlock.ContextMenu&gt;
                  &lt;ContextMenu&gt;
                      &lt;MenuItem Header="Peel"/&gt;
                  &lt;/ContextMenu&gt;
              &lt;/TextBlock.ContextMenu&gt;
          &lt;/TextBlock&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate DataType="{x:Type my:Apple}"&gt;
          &lt;TextBlock&gt;
              Apple
              &lt;TextBlock.ContextMenu&gt;
                  &lt;ContextMenu&gt;
                      &lt;MenuItem Header="Uncore"/&gt;
                  &lt;/ContextMenu&gt;
              &lt;/TextBlock.ContextMenu&gt;
          &lt;/TextBlock&gt;
      &lt;/DataTemplate&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid&gt;
      &lt;ListBox ItemsSource="{Binding Fruits}"/&gt;
  &lt;/Grid&gt;
  &lt;/Window&gt;
  
  
  using System.Windows;
  using System.Collections.ObjectModel;
  
  namespace WpfApplication8
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              Fruits = new ObservableCollection&lt;Fruit&gt;();
              Fruits.Add(new Apple());
              Fruits.Add(new Apple());
              Fruits.Add(new Orange());
              this.DataContext = this;
          }
  
          public ObservableCollection&lt;Fruit&gt; Fruits { get; set; }
      }
  
      public class Fruit
      {
      }
  
      public class Apple : Fruit
      {
      }
  
      public class Orange : Fruit
      {
      }
  }
  </code></pre>