title: Poor behavior/performance with context menu in WPF ListView
body: <p>I have a problem with a context menu appearing within a user control structure as shown in the follwing XAML.</p>
  
  <p>First, I have a user control representing a calendar. It consists of a 7x6 grid; each cell of the grid represents a date on the calendar. This XAML is a simplified version of my actual implmentation with details removed where I assume they are not relevant. Note that the calendar grid (ListView) is on a tab (not sure if that has any relevance to my problem, or the fact that this control operates inside of another level of tab in the host window).</p>
  
  <pre><code>&lt;UserControl x:Class="MyControl"
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="600" &gt;
      &lt;TabControl &gt;
          &lt;TabItem&gt;
              &lt;TabItem.HeaderTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;TextBlock Text="Calendar" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/TabItem.HeaderTemplate&gt;
                  &lt;Grid&gt;
                  &lt;ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled"&gt;
                      &lt;ListView.ItemsPanel&gt;
                          &lt;ItemsPanelTemplate&gt;
                              &lt;UniformGrid Rows="6" Columns="7" /&gt;
                          &lt;/ItemsPanelTemplate&gt;
                      &lt;/ListView.ItemsPanel&gt;
                      &lt;ListView.Resources&gt;
                          &lt;Style TargetType="{x:Type ListView}"&gt;
                              &lt;Setter Property="ItemContainerStyle"&gt;
                                  &lt;Setter.Value&gt;
                                      &lt;Style TargetType="ListViewItem"&gt;
                                          &lt;Setter Property="VerticalAlignment" Value="Stretch"/&gt;
                                          &lt;Setter Property="VerticalContentAlignment" Value="Stretch"/&gt;
                                          &lt;Setter Property="HorizontalAlignment" Value="Stretch"/&gt;
                                          &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt;
                                      &lt;/Style&gt;
                                  &lt;/Setter.Value&gt;
                              &lt;/Setter&gt;
                          &lt;/Style&gt;
                      &lt;/ListView.Resources&gt;
                      &lt;ListView.ItemTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;local:CalendarCell  Updated="CalendarCell_Updated"/&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/ListView.ItemTemplate&gt;
                  &lt;/ListView&gt;
              &lt;/Grid&gt;
          &lt;/TabItem&gt;
      &lt;/TabControl&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>Within each cell of the above grid is a user control (CalendarCell) representing a date on the calendar, as shown below. Part of the structure of this control is a ListView containing items to be displayed for a date:</p>
  
  <pre><code>&lt;UserControl x:Class="CalendarCell"
               d:DesignHeight="300" d:DesignWidth="300" &gt;
      &lt;UserControl.Resources&gt;
          &lt;SolidColorBrush x:Key="ThisMonthTextBrush" Color="Black" /&gt;
          &lt;SolidColorBrush x:Key="TodayTextBrush" Color="Red" /&gt;
          &lt;SolidColorBrush x:Key="OtherMonthTextBrush" Color="LightGray" /&gt;
          &lt;ContextMenu x:Key="ItemContextMenu"&gt;
              &lt;MenuItem Name="Edit" Header="Edit This Item" Click="Edit_Click" /&gt;
              &lt;MenuItem Name="Delete" Header="Delete This Item" Click="Delete_Click" /&gt;
          &lt;/ContextMenu&gt;
      &lt;/UserControl.Resources&gt;
      &lt;Border x:Name="CellBorder" BorderThickness="1" BorderBrush="Gray" Padding="3"&gt;
          &lt;Grid &gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="Auto"/&gt;
                  &lt;RowDefinition Height="*"/&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;ListView Grid.Row="1" ItemsSource="{Binding Items}" BorderBrush="Transparent" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionMode="Single" SelectionChanged="ListView_SelectionChanged"&gt;
                  &lt;ListView.ItemContainerStyle&gt;
                      &lt;Style TargetType="ListViewItem"&gt;
                          &lt;Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}"/&gt;
                      &lt;/Style&gt;
                  &lt;/ListView.ItemContainerStyle&gt;
                  &lt;ListView.ItemTemplate&gt;
                      &lt;DataTemplate &gt;
                          &lt;Border x:Name="MyBorder"  &gt;
                              &lt;TextBlock Padding="3" FontSize="12" Text="{Binding Path=Description}" ToolTip="{Binding Path=Note}" TextWrapping="Wrap" VerticalAlignment="Top"  /&gt;
                          &lt;/Border&gt;
                          &lt;DataTemplate.Triggers&gt;
                              &lt;DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsMouseOver}" Value="True"&gt;
                                  &lt;Setter TargetName="MyBorder" Property="Background" Value="LightGray"/&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsMouseOver}" Value="False"&gt;
                                  &lt;Setter TargetName="MyBorder" Property="Background" Value="White"/&gt;
                              &lt;/DataTrigger&gt;
                          &lt;/DataTemplate.Triggers&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ListView.ItemTemplate&gt;
              &lt;/ListView&gt;
              &lt;TextBlock x:Name="DayOfMonth" Padding="3" FontSize="16" FontWeight="Bold" Text="{Binding Path=Date.Day}" VerticalAlignment="Top" Foreground="{Binding Path=ItemBrush}" /&gt;
          &lt;/Grid&gt;
          &lt;Border.Style&gt;
              &lt;Style TargetType="Border"&gt;
                  &lt;Style.Triggers&gt;
                      &lt;DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"&gt;
                          &lt;Setter Property="BorderBrush" Value="Black"/&gt;
                          &lt;Setter Property="BorderThickness" Value="3"/&gt;
                      &lt;/DataTrigger&gt;
                  &lt;/Style.Triggers&gt;
              &lt;/Style&gt;
          &lt;/Border.Style&gt;
      &lt;/Border&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>The problem I have with all this is in the behavior of the context menu attached to the date's ListView items. The context menu only appears sporadically... if I try a few times, or hold the right button for a few seconds, I can get the menu to appear; a quick click (as one would normally do to invoke the context menu) rarely works. When the menu does appear, it works as expected... it just requires a lot of persistence and fiddling to get it to appear.</p>
  
  <p>My suspicion is that something is consuming the events before they can be delivered to the appropriate UI element, or that it may be a focus issue, but can't think of how to trace the cause of the issue.</p>
  
  <p>Note that I am using data triggers to add hover feedback (which works correctly)... doesn't alter context menu behavior if I remove the data triggers, so they don't appear to be related to the problem.</p>
  
  <p>By the way, this is VS2010 .NET 4 on Vista SP2.</p>