WPF: click on MenuItem, which menu was clicked?
Sep
14
Written by:
9/14/2012 2:47 PM
Familiar to click simply on the menu item on C# .Net 2.0, during the design, in order to implement the corresponding method, I was a few confused by the way to implement it on .Net 4.0, by using the WPF.
This post will give the solution on how to declare WPF Menuitems in order to be able to use them in the callback.
In fact, it is not so easy as for the Windows forms available on .Net 2.0. The name of the MenuItem should be have to be declared, and a test on the name of the MenuItem should be added in the callback, as follow. The MenuItem which was clicked in on the Source of the RoutedEventArgs:
Declared in the .xaml file:
<
MenuItem
Header
=
"_Help"
Click
=
"MenuHelp_Click"
>
<
MenuItem
Header
=
"About MyApp..."
Name
=
"mnuHelpAbout"
/>
<
MenuItem/>
In the .cs file:
private
void
MenuHelp_Click(
object
sender, RoutedEventArgs e)
MenuItem mnu = e.Source
as
MenuItem;
AboutBox dlgAbout =
new
AboutBox();
}
}