I am working on a WPF project and I have added a TreeView to it. I have also created a ContextMenu to the TreeView as below:
<TreeView Name="treeView" ItemsSource="{Binding Elements}">
<TreeView.ContextMenu>
<ContextMenu Name="treeViewContextMenu">
<MenuItem Header="First option"/>
<MenuItem Header="Second Option/>
</ContextMenu>
</TreeView.ContextMenu>
.... </TreeView>
Since I add the treeView nodes dinamically, how can I detect what node was right-clicked in order to open the contextMenu?
Hope someone can help me, thanks in advance
Assuming that I loaded my treeview item’s dynamically..
MainWindow.xaml.cs
It’s up to you if you want to know the object when the ContextMenu is Closed/Open or whatever event like when the MenuItem is Clicked.