title: ContextMenu in a ListView/GridView
body: <p>Inside a ListView, I have a GridView and am trying to set things up so that when the user right clicks a 'row' in the ListView, I'm able to access the original data object.
  I think its the gridview part of the ListView that is making this a bit more difficult.</p>
  
  <p>I've created a sample that demonstrates where I'm having difficulties. When the user right clicks the row eg Person1, I'd like to able to access the PersonClass data object in the MenuItem_Click handler. I've tried playing with PlacementTarget but all I get is null objects or objects of type MenuItem.</p>
  
  <pre><code>&lt;Window x:Class="MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"&gt;
  
      &lt;Window.Resources&gt;        
          &lt;ContextMenu Name="cm" x:Key="TestContextMenu"&gt;
              &lt;MenuItem Header="Context1" Click="MenuItem_Click"/&gt;
              &lt;MenuItem Header="Context2"/&gt;
              &lt;MenuItem Header="Context3"/&gt;
          &lt;/ContextMenu&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
          &lt;ListView Margin="20" Name="TestListView" SelectionMode="Multiple"&gt;
  
              &lt;ListView.ItemContainerStyle&gt;
                  &lt;Style TargetType="ListViewItem"&gt;
                      &lt;Setter Property="ContextMenu" Value="{StaticResource TestContextMenu}" /&gt;
                  &lt;/Style&gt;
              &lt;/ListView.ItemContainerStyle&gt;
  
              &lt;ListView.View&gt;
                  &lt;GridView&gt;
                      &lt;GridViewColumn Header="FirstName" Width="200" DisplayMemberBinding="{Binding Path=FirstName}"/&gt;
                      &lt;GridViewColumn Header="Surname" Width="200" DisplayMemberBinding="{Binding Path=Surname}"/&gt;
                  &lt;/GridView&gt;
              &lt;/ListView.View&gt;
  
          &lt;/ListView&gt;
      &lt;/Grid&gt;
  
  &lt;/Window&gt;
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>Class MainWindow 
  
      Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
  
          Dim Person1 As New PersonClass("John", "Fletcher")
          Dim Person2 As New PersonClass("Bob", "Smith")
  
          Dim ListOfPersons As New List(Of PersonClass)
          ListOfPersons.Add(Person1)
          ListOfPersons.Add(Person2)
  
          TestListView.ItemsSource = ListOfPersons
  
      End Sub
  
      Private Sub MenuItem_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
          MsgBox(e.OriginalSource.ToString)
          MsgBox(sender.ToString)
          MsgBox(e.Source.ToString)
  
  
      End Sub
  
  End Class
  
  
  Public Class PersonClass
  
      Private _firstName As String
      Private _surname As String
  
      Public Property FirstName() As String
          Get
              Return _firstName
          End Get
          Set(ByVal Value As String)
              _firstName = Value
          End Set
      End Property
  
      Public Property Surname() As String
          Get
              Return _surname
          End Get
          Set(ByVal Value As String)
              _surname = Value
          End Set
      End Property
  
      Public Sub New()
      End Sub
  
      Public Sub New(FirstName As String, Surname As String)
          Me.FirstName = FirstName
          Me.Surname = Surname
      End Sub
  
  End Class
  </code></pre>