Answered by:
WPF click event never fires!!!!

Question
-
I have a simple menu in my WPF applications and no event want to fire. I tried both ways, either double clicking the corresponding event at the "events" windows besides properties, or give a manual event to the XAML. Neither work. I can't even fire a normal click event to the menu itself nor the menu items!!! My XAML code is as follow:
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="438" Width="658" Name="winMain" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:Tutorial_Keeper_Real"> <Grid Height="410" Name="grdMain" Width="646"> <Grid.RowDefinitions> <RowDefinition Height="25" /> <RowDefinition /> </Grid.RowDefinitions> <DockPanel Name="docPanMain" LastChildFill="False"> <Menu Height="34" Name="Menu1" Width="648"> <MenuItem Header="File" Name="mnuFile"> <MenuItem Header="New Tutorial" Name="mnuItemNewTutorial"/> </MenuItem> <MenuItem Header="Search" Name="mnuSearch"> <MenuItem Header="Search Tutorial" /> </MenuItem> <MenuItem Header="Edit" Name="mnuEdit"> <MenuItem Header="Cloud Services"> <MenuItem Header="Sky Drive" /> </MenuItem> </MenuItem> <MenuItem Header="Help" Name="mnuHelp" /> </Menu> </DockPanel> </Grid> </Window>
I know I can add the event at the XAML and then program the behavior at code behind. I just don't know what possible could go wrong. I'm not that noob >_>. I'm using Visual Basic Express 2010. Please help!!
Friday, November 8, 2013 4:19 AM
Answers
-
The click event only fires if there are no submenu. If you want fire an event on the File button, you have to use some of the mouse events like the MouseDown event or the PreviewMouseDown event:
<Menu Height="34" Name="Menu1" Width="648"> <MenuItem Header="File" MouseDown="MouseButtonEventHandler" Name="mnuFile"> <MenuItem Header="New Tutorial" Name="mnuItemNewTutorial"/> </MenuItem> <!-- ... --> </Menu>
Chris
Code Samples: Code SamplesHi,
As chriga suggested, you should use mouse event to handle this, but we can just use PreviewMouseDown event, MouseDown event cannot work:
<MenuItem Header="File" Name="mnuFile"> <MenuItem Header="New Tutorial" Name="mnuItemNewTutorial" /> </MenuItem>
Private Sub mnuFile_PreviewMouseDown(sender As Object, e As MouseButtonEventArgs) Handles mnuFile.PreviewMouseDown MessageBox.Show("PreviewMouseDown:" + TryCast(sender, MenuItem).Header) End Sub
Please check the following references:
#UIElement.PreviewMouseDown Event
http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewmousedown.aspxWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Proposed as answer by Leo (Apple) Yang Monday, November 18, 2013 10:02 AM
- Marked as answer by Franklin ChenMicrosoft employee Monday, November 18, 2013 10:22 AM
Friday, November 8, 2013 9:06 AM
All replies
-
I have a simple menu in my WPF applications and no event want to fire. I tried both ways, either double clicking the corresponding event at the "events" windows besides properties, or give a manual event to the XAML. Neither work. I can't even fire a normal click event to the menu itself nor the menu items!!! My XAML code is as follow:
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="438" Width="658" Name="winMain" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:Tutorial_Keeper_Real"> <Grid Height="410" Name="grdMain" Width="646"> <Grid.RowDefinitions> <RowDefinition Height="25" /> <RowDefinition /> </Grid.RowDefinitions> <DockPanel Name="docPanMain" LastChildFill="False"> <Menu Height="34" Name="Menu1" Width="648"> <MenuItem Header="File" Name="mnuFile"> <MenuItem Header="New Tutorial" Name="mnuItemNewTutorial"/> </MenuItem> <MenuItem Header="Search" Name="mnuSearch"> <MenuItem Header="Search Tutorial" /> </MenuItem> <MenuItem Header="Edit" Name="mnuEdit"> <MenuItem Header="Cloud Services"> <MenuItem Header="Sky Drive" /> </MenuItem> </MenuItem> <MenuItem Header="Help" Name="mnuHelp" /> </Menu> </DockPanel> </Grid> </Window>
I know I can add the event at the XAML and then program the behavior at code behind. I just don't know what possible could go wrong. I'm not that noob >_>. I'm using Visual Basic Express 2010. Please help!!
Now I was able to add a "click" event to the menu item, but I still cannot fire a click event when I actually click the "File button" at the menu. I just want to show a Message Box for test purposes.Friday, November 8, 2013 4:38 AM -
The click event only fires if there are no submenu. If you want fire an event on the File button, you have to use some of the mouse events like the MouseDown event or the PreviewMouseDown event:
<Menu Height="34" Name="Menu1" Width="648"> <MenuItem Header="File" MouseDown="MouseButtonEventHandler" Name="mnuFile"> <MenuItem Header="New Tutorial" Name="mnuItemNewTutorial"/> </MenuItem> <!-- ... --> </Menu>
Chris
Code Samples: Code SamplesFriday, November 8, 2013 7:33 AM -
The click event only fires if there are no submenu. If you want fire an event on the File button, you have to use some of the mouse events like the MouseDown event or the PreviewMouseDown event:
<Menu Height="34" Name="Menu1" Width="648"> <MenuItem Header="File" MouseDown="MouseButtonEventHandler" Name="mnuFile"> <MenuItem Header="New Tutorial" Name="mnuItemNewTutorial"/> </MenuItem> <!-- ... --> </Menu>
Chris
Code Samples: Code SamplesHi,
As chriga suggested, you should use mouse event to handle this, but we can just use PreviewMouseDown event, MouseDown event cannot work:
<MenuItem Header="File" Name="mnuFile"> <MenuItem Header="New Tutorial" Name="mnuItemNewTutorial" /> </MenuItem>
Private Sub mnuFile_PreviewMouseDown(sender As Object, e As MouseButtonEventArgs) Handles mnuFile.PreviewMouseDown MessageBox.Show("PreviewMouseDown:" + TryCast(sender, MenuItem).Header) End Sub
Please check the following references:
#UIElement.PreviewMouseDown Event
http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewmousedown.aspxWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Proposed as answer by Leo (Apple) Yang Monday, November 18, 2013 10:02 AM
- Marked as answer by Franklin ChenMicrosoft employee Monday, November 18, 2013 10:22 AM
Friday, November 8, 2013 9:06 AM -
The event you want is SubMenuOpened. In this event you can use the Handled property in the RoutedEventArgs to show that your code handled the open if you want to "cancel" the normal processing.
Lloyd Sheen
Friday, November 8, 2013 11:14 AM -
Hi,
Since we haven't heard from you for a long time, I temporarily close this case. I mark useful reply as answer. If you have any concerns, please free feel to reopen it or submit a new question.
Thanks for your understanding.
Best regards,
FranklinWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.Monday, November 18, 2013 10:22 AM