title: WPF: Displaying a Context Menu for a GridView's Items
body: <p>I have the following <code>GridView</code>:</p>
  
  <pre><code>&lt;ListView Name="TrackListView" ItemContainerStyle="{StaticResource itemstyle}"&gt;
      &lt;ListView.View&gt;
          &lt;GridView&gt;
              &lt;GridViewColumn Header="Title" Width="100" HeaderTemplate="{StaticResource BlueHeader}" DisplayMemberBinding="{Binding Name}"/&gt;
              &lt;GridViewColumn Header="Artist" Width="100" HeaderTemplate="{StaticResource BlueHeader}" DisplayMemberBinding="{Binding Album.Artist.Name}" /&gt;
              &lt;GridViewColumn Header="Album" Width="100" HeaderTemplate="{StaticResource BlueHeader}" DisplayMemberBinding="{Binding Album.Name}"/&gt;
              &lt;GridViewColumn Header="Length" Width="100" HeaderTemplate="{StaticResource BlueHeader}"/&gt;
          &lt;/GridView&gt;
       &lt;/ListView.View&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>Now I would like to display a context menu on a right click on a bounded item that will allow me to retrieve the item selected when I handle the event in the code behind.</p>
  
  <p>In what possible way can I accomplish this?  </p>
  
  <hr>
  
  <p><strong>[Update]</strong></p>
  
  <p>Following <a href="https://stackoverflow.com/questions/747872/wpf-displaying-a-context-menu-for-a-gridviews-items/749671#749671">Dennis Roche</a>'s code, I now have this:</p>
  
  <pre><code>    &lt;ListView Name="TrackListView" ItemContainerStyle="{StaticResource itemstyle}"&gt;
          &lt;ListView.ItemContainerStyle&gt;
              &lt;Style TargetType="{x:Type ListViewItem}"&gt;
                  &lt;EventSetter Event="PreviewMouseLeftButtonDown" Handler="OnListViewItem_PreviewMouseLeftButtonDown" /&gt;
                  &lt;Setter Property="ContextMenu"&gt;
                      &lt;Setter.Value&gt;
                          &lt;ContextMenu&gt;
                              &lt;MenuItem Header="Add to Playlist"&gt;&lt;/MenuItem&gt;
                          &lt;/ContextMenu&gt;
                       &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Style&gt;
          &lt;/ListView.ItemContainerStyle&gt;
  
          &lt;ListView.View&gt;
              &lt;GridView&gt;
                  &lt;GridViewColumn Header="Title" Width="100" HeaderTemplate="{StaticResource BlueHeader}" DisplayMemberBinding="{Binding Name}"/&gt;
                  &lt;GridViewColumn Header="Artist" Width="100" HeaderTemplate="{StaticResource BlueHeader}" DisplayMemberBinding="{Binding Album.Artist.Name}" /&gt;
                  &lt;GridViewColumn Header="Album" Width="100" HeaderTemplate="{StaticResource BlueHeader}" DisplayMemberBinding="{Binding Album.Name}"/&gt;
                  &lt;GridViewColumn Header="Length" Width="100" HeaderTemplate="{StaticResource BlueHeader}"/&gt;
              &lt;/GridView&gt;
           &lt;/ListView.View&gt;
      &lt;/ListView&gt;
  </code></pre>
  
  <p>But upon running, I am receiving this exception:</p>
  
  <blockquote>
    <p>Cannot add content of type
    'System.Windows.Controls.ContextMenu'
    to an object of type 'System.Object'. 
    Error at object
    'System.Windows.Controls.ContextMenu'
    in markup file
    'MusicRepo_Importer;component/controls/trackgridcontrol.xaml'.</p>
  </blockquote>
  
  <p>What is the problem?</p>