title: How to prevent a WPF context menu from appearing on the empty space of a tree view?
body: <p>I have a simple context menu on a dynamically populated tree view.</p>
  
  <p>How can I prevent this context menu from appearing if I right click on empty space (below the last item) ? I tried to use the ContextMenuOpening event, but it is <a href="http://connect.microsoft.com/VisualStudio/feedback/details/353112/contextmenu-opening-event-doesnt-fire-properly" rel="nofollow">buggy</a> and isn't raised. </p>
  
  <hr>
  
  <pre><code>            &lt;TreeView.ContextMenu&gt;
                  &lt;ContextMenu&gt;
                      &lt;MenuItem Header="_Open" Click="MenuItemOpen_Click" /&gt;
                  &lt;/ContextMenu&gt;
              &lt;/TreeView.ContextMenu&gt;
              &lt;TreeView.ItemContainerStyle&gt;
                  &lt;Style TargetType="TreeViewItem"&gt;
                      &lt;Setter Property="IsExpanded" Value="True" /&gt;
                      &lt;!-- Select node on right click : --&gt;                        
                      &lt;EventSetter Event="TreeViewItem.PreviewMouseRightButtonDown" Handler="TreeViewItem_PreviewMouseRightButtonDown"/&gt;
                  &lt;/Style&gt;
              &lt;/TreeView.ItemContainerStyle&gt;
  </code></pre>