ContextMenu in WPF?

You can define your ContextMenu in resources and bind it to any control you needed. Check this out.

Up vote 3 down vote favorite share g+ share fb share tw.

I'm new in WPF, though I have some experience with WinForms. I just want to add ContextMenu for several objects that I create dynamically, but I found no ContextMenu component in VS toolbox. 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? C# wpf visual-studio-2008 contextmenu link|improve this question asked Sep 28 '11 at 11:31Michael3426 60% accept rate.

– Michael Sep 28 '11 at 11:55 @Michael: You should be able to get the ContextMenu at runtime via var pMenu = (ContextMenu)this. Resources"MyContextMenu"; Then you can assign this variable to your dynamically created control. – Heinzi Sep 28 '11 at 12:05 Thanks, it works.

The only thing I cannot understand is why in WPF I have to do all this magic and there is no appropriate component and GUI in IDE to make things much easier as they must be. – Michael Sep 28 '11 at 12:22 1 @Michael In WPF, your forms are not your application. Your data is.

This is quite different from WinForms where your forms are your application and the data gets added to form objects. In WPF, your data is your application and your form objects are just a pretty layer to allow users to interact with your data layer. Once you get used to that, WPF becomes much easier :) – Rachel Sep 28 '11 at 13:01.

Additionaly you can put commands on the menuItem... Like this: CommandTarget can be very important, if you use your contextMenu on different controls. I often use the FindAncestor here, to identify the caller.

I use PlacementTarget to get the caller – Michael Sep 28 '11 at 21:57.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions