title: Find Bound Item from TreeViewItem from ContextMenu
body: <p>I have this Tree View that looks like this:</p>
  
  <pre><code>        &lt;TreeView 
                  Grid.Column="0"
                  Grid.Row="2"
                  MinHeight="100" 
                  MinWidth="100"
                  BorderBrush="White"
                  DataContext="{Binding Projects, Source={x:Static SizingApp:Manager.Instance}}"&gt;
                  &lt;TreeView.Resources&gt;
                      &lt;Style TargetType="TreeViewItem"&gt;
                          &lt;Setter Property="IsExpanded" Value="True" /&gt;
                      &lt;/Style&gt;
                      &lt;DataTemplate x:Key="LoadTemplate"&gt;
                          &lt;TextBlock Text="{Binding Name}"&gt;
                              &lt;TextBlock.ContextMenu&gt;
                                  &lt;ContextMenu&gt;
                                      &lt;MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Edit_InputGestureText}"&gt;
                                          &lt;MenuItem.Header&gt;
                                              &lt;TextBlock Text="{Binding Path=Name, 
                                                                              StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Edit_Header}}" /&gt;
                                          &lt;/MenuItem.Header&gt;
                                      &lt;/MenuItem&gt;
                                      &lt;MenuItem&gt;
                                          &lt;MenuItem.Header&gt;
                                              &lt;TextBlock Text="{Binding Path=Name, 
                                                                              StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Save_Header}}" /&gt;
                                          &lt;/MenuItem.Header&gt;
                                      &lt;/MenuItem&gt;
                                      &lt;MenuItem&gt;
                                          &lt;MenuItem.Header&gt;
                                              &lt;TextBlock Text="{Binding Path=Name, 
                                                                              StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Export_Header}}" /&gt;
                                          &lt;/MenuItem.Header&gt;
                                      &lt;/MenuItem&gt;
                                      &lt;MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Copy_Header}"
                                                                      InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Copy_InputGestureText}"/&gt;
                                      &lt;MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Remove_InputGestureText}"&gt;
                                          &lt;MenuItem.Header&gt;
                                              &lt;TextBlock Text="{Binding Path=Name, 
                                                                              StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Remove_Header}}" /&gt;
                                          &lt;/MenuItem.Header&gt;
                                      &lt;/MenuItem&gt;
                                  &lt;/ContextMenu&gt;
                              &lt;/TextBlock.ContextMenu&gt;
                          &lt;/TextBlock&gt;
                      &lt;/DataTemplate&gt;
                      &lt;HierarchicalDataTemplate x:Key="StepTemplate"
                              ItemsSource="{Binding Loads}" 
                              ItemTemplate="{StaticResource LoadTemplate}"&gt;
                          &lt;TextBlock Text="{Binding Name}"&gt;
                              &lt;TextBlock.ContextMenu&gt;
                                  &lt;ContextMenu&gt;
                                      &lt;MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Edit_InputGestureText}"&gt;
                                          &lt;MenuItem.Header&gt;
                                              &lt;TextBlock Text="{Binding Path=Name, 
                                                                  StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Edit_Header}}" /&gt;
                                          &lt;/MenuItem.Header&gt;
                                      &lt;/MenuItem&gt;
                                      &lt;MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Copy_Header}" 
                                                          InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Copy_InputGestureText}" /&gt;
                                      &lt;MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Paste_Header}" 
                                                          InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Paste_InputGestureText}" /&gt;
                                      &lt;MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Import_Load_Header}" /&gt;
                                      &lt;MenuItem&gt;
                                          &lt;MenuItem.Header&gt;
                                              &lt;TextBlock Text="{Binding Path=Name, 
                                                                  StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Export_Header}}" /&gt;
                                          &lt;/MenuItem.Header&gt;
                                      &lt;/MenuItem&gt;
                                      &lt;MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Remove_InputGestureText}"&gt;
                                          &lt;MenuItem.Header&gt;
                                              &lt;TextBlock Text="{Binding Path=Name, 
                                                                  StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Remove_Header}}" /&gt;
                                          &lt;/MenuItem.Header&gt;
                                      &lt;/MenuItem&gt;
                                      &lt;MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Add_Load_Header}"
                                                          ItemsSource="{StaticResource mnuLoadSubmenuCommon}"/&gt;
                                  &lt;/ContextMenu&gt;
                              &lt;/TextBlock.ContextMenu&gt;
                          &lt;/TextBlock&gt;
                      &lt;/HierarchicalDataTemplate&gt;
                      &lt;HierarchicalDataTemplate x:Key="ProjectTemplate"
                              ItemsSource="{Binding Steps}"
                              ItemTemplate="{StaticResource StepTemplate}"&gt;
                          &lt;TextBlock Text="{Binding Name}"&gt;
                              &lt;TextBlock.ContextMenu&gt;
                                  &lt;ContextMenu&gt;
                                      &lt;MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Edit_InputGestureText}" 
                                                Click="EditProject_Click"&gt;
                                          &lt;MenuItem.Header&gt;
                                              &lt;TextBlock Text="{Binding Path=Name, 
                                                      StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Edit_Header}}" /&gt;
                                          &lt;/MenuItem.Header&gt;
                                      &lt;/MenuItem&gt;
                                      &lt;MenuItem&gt;
                                          &lt;MenuItem.Header&gt;
                                              &lt;TextBlock Text="{Binding Path=Name,
                                                      StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Save_Header}}" /&gt;
                                          &lt;/MenuItem.Header&gt;
                                      &lt;/MenuItem&gt;
                                      &lt;MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Copy_Header}" 
                                              InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Copy_InputGestureText}" /&gt;
                                      &lt;MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Paste_Header}" 
                                              InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Paste_InputGestureText}" /&gt;
                                      &lt;MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Remove_InputGestureText}"&gt;
                                          &lt;MenuItem.Header&gt;
                                              &lt;TextBlock Text="{Binding Path=Name,
                                                      StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Remove_Header}}" /&gt;
                                          &lt;/MenuItem.Header&gt;
                                      &lt;/MenuItem&gt;
                                      &lt;MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Project_AddStep_Header}" /&gt;
                                      &lt;MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Project_ImportStep_Header}" /&gt;
                                  &lt;/ContextMenu&gt;
                              &lt;/TextBlock.ContextMenu&gt;
                          &lt;/TextBlock&gt;
                      &lt;/HierarchicalDataTemplate&gt;
                  &lt;/TreeView.Resources&gt;
                  &lt;TreeViewItem 
                          Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Header}"
                          ItemsSource="{Binding}"
                          IsExpanded="True"
                          ItemTemplate="{StaticResource ProjectTemplate}"&gt;
                      &lt;TreeViewItem.ContextMenu&gt;
                          &lt;ContextMenu&gt;
                              &lt;MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Paste_Header}" 
                                      InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Paste_InputGestureText}" /&gt;
                          &lt;/ContextMenu&gt;
                      &lt;/TreeViewItem.ContextMenu&gt;
                  &lt;/TreeViewItem&gt;
              &lt;/TreeView&gt;
  </code></pre>
  
  <p>And here is my click event</p>
  
  <pre><code>    private void EditProject_Click(object sender, RoutedEventArgs e)
      {
          //how do I find the bound item of the node this context menu resides on?
      }
  </code></pre>
  
  <p>I am trying to write the EditProject_Click event for the ContextMenu MenuItem click event. When I click it, I want to be able to find the item Bound to the node that the context menu resides. How do I do that?</p>
  
  <hr>
  
  <p>The answer</p>
  
  <pre><code>    private void EditProject_Click(object sender, RoutedEventArgs e)
      {
          Project project = (Project)((MenuItem)sender).DataContext;
  
          //Do stuff with the project here
      }
  </code></pre>
  
  <p>Also, if you want to get your ContextMenu to update it's DataContext from the element the ContextMenu is suppose to be attached to, here is the syntax for that:</p>
  
  <pre><code>&lt;ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"&gt;
  </code></pre>