I am trying to dynamically populate a Menu control (which is in the ControlTemplate) with various MenuItems (all created in the code), but I am encountering some strange problems. If all MenuItems are created as root elements, they successfully fire the Click event. But as soon as any MenuItem becomes a child of another MenuItem, the parent and child Click events stop firing.
XAML:
<ControlTemplate> <Menu x:Name="MyMenu"/> </ControlTemplate>
WITH#:
// This is in the control that the above ControlTemplate is created for public override void OnApplyTemplate() { base.OnApplyTemplate(); MenuItem L1 = new MenuItem() { Header = "L1" }; MyMenu.Items.Add(L1); // Add as root L1.Click += new RoutedEventHandler(delegate { MessageBox.Show("L1 Click"); }); MenuItem L2 = new MenuItem() { Header = "L2" }; L1.Items.Add(L2); // Add as child of L1. // Note: If this is a child of MyMenu, both MenuItems work as expected L2.Click += new RoutedEventHandler(delegate { MessageBox.Show("L2 Click"); }); }
The menu is displayed correctly, but none of the pressing events on the sub-items works at all. If I pre-define the menu in XAML and set all the Click events in XAML, it works fine - but it should be in the code, so this is not an option. In addition, if I make βL2β a child of βMyMenuβ, that is, make it the root element, L1 and L2 will start working again, but I cannot have everything as root elements. Is there something I'm missing?
Thanks!
EDIT 1:
I tried a little experiment to make sure everything was properly initialized before bringing up L1-L2 and adding their Click events. Not lucky yet. Here is what I tried:
XAML:
<ControlTemplate> <Grid> <Menu x:Name="MyMenu"/> <Button x:Name="MyButton"/> </Grid> </ControlTemplate>
WITH#:
public override void OnApplyTemplate() { base.OnApplyTemplate(); MenuItem L1 = new MenuItem() { Header = "L1" }; MyMenu.Items.Add(L1); MenuItem L2 = new MenuItem() { Header = "L2" }; MyMenu.Items.Add(L2);
When the window loads and everything is displayed, I click on the button so that the parent event happens and events are added. I see that L2 moves as a child of L1, but when I click either, they do not respond to Click events. If I do not allow a form of parenting to take place, they respond to Click events. I am so confused why this is happening!
EDIT 2:
I reproduced everything from the original post in a clean project, and everything works fine. Therefore, this is not a problem with Menu or MenuItems or the way they were used. The cause of this problem is still unknown ...
EDIT 3:
As requested, I re-tested this by adding the following code to the window:
PreviewMouseLeftButtonDown += new MouseButtonEventHandler(delegate { // Use Ctrl key to enable MessageBox so focus is not lost when opening menu if (Keyboard.Modifiers == ModifierKeys.Control) MessageBox.Show("Window PreviewMouseLeftButtonDown"); });
PreviewMouseLeftButtonDown always starts when a ctrl-click on the "dead" MenuItems. Their Click events continue to shut down after being parented. There is still no solution or indication of the problem ...
EDIT 4:
I performed the following test by adding below code for L1 and L2:
L1.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(delegate { MessageBox.Show("L1 PreviewMouseLeftButtonDown"); });
When Clicked, MenuItem L1 and L2 respond to PreviewMouseLeftButtonDown, but continue to stop responding to Click after being parent.