.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > ContextMenu from code - how do I create an overall click event?
 

ContextMenu from code - how do I create an overall click event?

Hi there,
I'd like to create a ContextMenu in code, with 1 overall click event. If I was to create it in XAML it would look like this:

<Polygon.ContextMenu>
<ContextMenu MenuItem.Click="MyClickEvent">
<MenuItem Header="Red" />
<MenuItem Header="Blue"/>
<MenuItem Header="Green"/>
</ContextMenu>
</Polygon.ContextMenu>

In code it looks like this:

ContextMenu cm = new ContextMenu();
MenuItem mi = new MenuItem();
mi.Header = "Red";
cm.Items.Add(mi);
mi = new MenuItem();
mi.Header = "Blue";
cm.Items.Add(mi);
mi = new MenuItem();
mi.Header = "Green";
cm.Items.Add(mi);
polygon.ContextMenu = cm;

What's the code to add the overall MyClickEvent in code?

Apologies if this in a n00b question, I haven't been doing any WPF over the last few months and am only slowly starting to get back into this way of thinking. Thanks
  • Edited byNeal Hudson Wednesday, July 02, 2008 3:33 PMFlagging as containing code sample
  •  
Neal Hudson
I am not sure if the above answers the question. So, here is my try. You would do this to listen to click events from the ContextMenu rather than each menuitem:

publicWindow1()
{
InitializeComponent();
this.myCM.AddHandler(MenuItem.ClickEvent,newRoutedEventHandler(mi_Click));
}
voidmi_Click(objectsender,RoutedEventArgse)
{
//code
}

Praveen
  • Proposed As Answer byPraveen.R Wednesday, July 02, 2008 3:57 PM
  • Marked As Answer byNeal Hudson Wednesday, July 02, 2008 4:33 PM
  •  
Praveen.R
mi.Click+=newRoutedEventHandler(mi_Click);
voidmi_Click(objectsender,RoutedEventArgse)
{
//code
}

Most Components have events that are added and deleted using applicable operators (+,+=,-,-=). You append a void *EventHandler class to the Click Event Handler, and then create and code that method.

ContextMenu does not have this event.
  • Edited bys-mc Wednesday, July 02, 2008 3:45 PMclear up
  •  
s-mc
I am not sure if the above answers the question. So, here is my try. You would do this to listen to click events from the ContextMenu rather than each menuitem:

publicWindow1()
{
InitializeComponent();
this.myCM.AddHandler(MenuItem.ClickEvent,newRoutedEventHandler(mi_Click));
}
voidmi_Click(objectsender,RoutedEventArgse)
{
//code
}

Praveen
  • Proposed As Answer byPraveen.R Wednesday, July 02, 2008 3:57 PM
  • Marked As Answer byNeal Hudson Wednesday, July 02, 2008 4:33 PM
  •  
Praveen.R
s-mc said:

mi.Click+=newRoutedEventHandler(mi_Click);
voidmi_Click(objectsender,RoutedEventArgse)
{
//code
}

Most Components have events that are added and deleted using applicable operators (+,+=,-,-=). You append a void *EventHandler class to the Click Event Handler, and then create and code that method.


This would mean adding the click event to each individual MenuItem rather than at the ContextMenu level, which you can do with XAML
Neal Hudson

You can use google to search for other answers

Custom Search

More Threads

• WPF: How can we figure out the real width of textblock in multilanguage?
• Is there a true Screen.WorkingArea.Width in WPF?? Problems with DPI
• Little WPF / XAML problem
• Problems with SoundPlayerAction
• Possible .net framework bug, regarding the tooltips
• Error AccessViolationExeption
• Change TextBlock Text in a ControlTemplate in CodeBehind
• When to use ViewBox and Grid
• Databinding to the IsVisible Property
• WPF 3D Graphics and Lines