Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would have a treeview that when right clicked on brings up a ContextMenu with several items. I also have a MainWindowViewModel class that serves as the data context for the window that gets generated when the program is run. Does anyone know how to bind the command of a ContextMenu MenuItem to an ICommand member of that viewmodel class? I would like to be able to add/delete tree nodes via this menu.


1 solution

I kind of cheated by manually setting my contextMenu's datacontext to the same one the mainwindow was using by creating an event handler for the ContextMenu's "Opened" event.

Here's the code from MainWindow.xaml.cs:

private void ContextMenu_Opened(object sender, System.Windows.RoutedEventArgs e)
            ContextMenu menu = sender as ContextMenu;
            menu.DataContext = DataContext;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900