I have this Tree View that looks like this:
<TreeView
Grid.Column="0"
Grid.Row="2"
MinHeight="100"
MinWidth="100"
BorderBrush="White"
DataContext="{Binding Projects, Source={x:Static SizingApp:Manager.Instance}}">
<TreeView.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
</Style>
<DataTemplate x:Key="LoadTemplate">
<TextBlock Text="{Binding Name}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Edit_InputGestureText}">
<MenuItem.Header>
<TextBlock Text="{Binding Path=Name,
StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Edit_Header}}" />
</MenuItem.Header>
</MenuItem>
<MenuItem>
<MenuItem.Header>
<TextBlock Text="{Binding Path=Name,
StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Save_Header}}" />
</MenuItem.Header>
</MenuItem>
<MenuItem>
<MenuItem.Header>
<TextBlock Text="{Binding Path=Name,
StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Export_Header}}" />
</MenuItem.Header>
</MenuItem>
<MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Copy_Header}"
InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Copy_InputGestureText}"/>
<MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Remove_InputGestureText}">
<MenuItem.Header>
<TextBlock Text="{Binding Path=Name,
StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Remove_Header}}" />
</MenuItem.Header>
</MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</DataTemplate>
<HierarchicalDataTemplate x:Key="StepTemplate"
ItemsSource="{Binding Loads}"
ItemTemplate="{StaticResource LoadTemplate}">
<TextBlock Text="{Binding Name}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Edit_InputGestureText}">
<MenuItem.Header>
<TextBlock Text="{Binding Path=Name,
StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Edit_Header}}" />
</MenuItem.Header>
</MenuItem>
<MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Copy_Header}"
InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Copy_InputGestureText}" />
<MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Paste_Header}"
InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Paste_InputGestureText}" />
<MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Import_Load_Header}" />
<MenuItem>
<MenuItem.Header>
<TextBlock Text="{Binding Path=Name,
StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Export_Header}}" />
</MenuItem.Header>
</MenuItem>
<MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Remove_InputGestureText}">
<MenuItem.Header>
<TextBlock Text="{Binding Path=Name,
StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Remove_Header}}" />
</MenuItem.Header>
</MenuItem>
<MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Add_Load_Header}"
ItemsSource="{StaticResource mnuLoadSubmenuCommon}"/>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="ProjectTemplate"
ItemsSource="{Binding Steps}"
ItemTemplate="{StaticResource StepTemplate}">
<TextBlock Text="{Binding Name}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Edit_InputGestureText}"
Click="EditProject_Click">
<MenuItem.Header>
<TextBlock Text="{Binding Path=Name,
StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Edit_Header}}" />
</MenuItem.Header>
</MenuItem>
<MenuItem>
<MenuItem.Header>
<TextBlock Text="{Binding Path=Name,
StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Save_Header}}" />
</MenuItem.Header>
</MenuItem>
<MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Copy_Header}"
InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Copy_InputGestureText}" />
<MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Paste_Header}"
InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Paste_InputGestureText}" />
<MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Remove_InputGestureText}">
<MenuItem.Header>
<TextBlock Text="{Binding Path=Name,
StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Remove_Header}}" />
</MenuItem.Header>
</MenuItem>
<MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Project_AddStep_Header}" />
<MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Project_ImportStep_Header}" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</HierarchicalDataTemplate>
</TreeView.Resources>
<TreeViewItem
Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Header}"
ItemsSource="{Binding}"
IsExpanded="True"
ItemTemplate="{StaticResource ProjectTemplate}">
<TreeViewItem.ContextMenu>
<ContextMenu>
<MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Paste_Header}"
InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Paste_InputGestureText}" />
</ContextMenu>
</TreeViewItem.ContextMenu>
</TreeViewItem>
</TreeView>
And here is my click event
private void EditProject_Click(object sender, RoutedEventArgs e)
{
//how do I find the bound item of the node this context menu resides on?
}
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?
The answer
private void EditProject_Click(object sender, RoutedEventArgs e)
{
Project project = (Project)((MenuItem)sender).DataContext;
//Do stuff with the project here
}
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:
<ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">