I have a ContextMenu that appears on a right click of a TreeViewItem. What I would like to do now is pass a couple of details about the TreeViewItem to the context menu.
How would I do that starting from here:
XAML
<TreeView x:Class="MyApp.TreeControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
PreviewMouseLeftButtonDown="TreeView_PreviewMouseLeftButtonDown"
PreviewMouseMove="TreeView_PreviewMouseMove"
PreviewMouseRightButtonDown="TreeControl_OnPreviewMouseRightButtonDown">
<TreeView.ContextMenu>
<ContextMenu>
<MenuItem Name="dataItem1" Header="Property1"></MenuItem>
<MenuItem Name="dataItem2" Header="Property2"></MenuItem>
<Separator/>
<MenuItem Name="taskItem" Header="Tasks" Click="TaskItem_OnClick"></MenuItem>
</ContextMenu>
</TreeView.ContextMenu>
</TreeView>
I would like the words Property1
and Property2
replaced by the values of those properties in the TreeViewItem that was right clicked, sort of like : {binding selectedItem.Property1}
Code: (basically to highlight the clicked node)
private void TreeControl_OnPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
TreeViewItem treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject);
if (treeViewItem != null)
{
treeViewItem.IsSelected = true;
treeViewItem.Focus();
e.Handled = true;
}
}