I just want to add ContextMenu for several objects that I create dynamically, but
The only way I found is to create ContextMenu in runtime like this:
ContextMenu pMenu = new ContextMenu();
MenuItem item1 = new MenuItem();
MenuItem item2 = new MenuItem();
//I have about 10 items
//...
item1.Header = "item1";
item1.Click += new RoutedEventHandler(item1_Click);
pMenu.Items.Add(item1);
item2.Header = "item2";
item2.Click += new RoutedEventHandler(item2_Click);
pMenu.Items.Add(item2);
//and so on
It works, however, in WinForms I was able to drop ContextMenuStrip component to my form and define items and events very quickly w/o writing any code. Is it possible in WPF?
You can define your
ContextMenuin resources and bind it to any control you needed. Check this out: