Q:

WPF MenuItem.Click breaks after parenting

I am trying to dynamically populate a Menu control (which resides in a ControlTemplate) with various MenuItems (all created in code) but I am running into some weird problems. If the MenuItems are all created as root items, they fire their Click event successfully. But as soon as any MenuItem is made a child of another MenuItem, both the parent and child's Click events stop firing.
XAML:
<ControlTemplate>
    <Menu x:Name="MyMenu"/>
</ControlTemplate>

C#:
// 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 displays correctly, but none of the click events on the sub-items fire at all. If I pre-define the menu in XAML and set all Click events in XAML too, it works perfectly - but it has to be in code, so this is not an option. Also, if I make 'L2' to be a child of 'MyMenu', i.e. make it a root item instead, L1 and L2 start to work again - but I cannot have everything as root items either.
Is there something that I am missing?
Thank you!
EDIT 1:
I have tried a little experiment to see if everything was properly initialised before parenting L1 to L2 and adding their Click events. Still no luck. Here is what I tried:
XAML:
<ControlTemplate>
    <Grid>
        <Menu x:Name="MyMenu"/>
        <Button x:Name="MyButton"/>
    </Grid>
</ControlTemplate>

C#:
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); // Add this to the menu to make sure it gets initialised

    // I created a button so I can make sure that the parenting
    // and events are only added after the MenuItems are loaded
    MyButton.Click += new RoutedEventHandler(MyButton_Clicked);
}

void MyButton_Clicked(object sender, RoutedEventArgs e)
{
    // Note: If these two lines are removed, the events work fine
    MyMenu.Items.Remove(L2);
    L1.Items.Add(L2);

    L1.Click += new RoutedEventHandler(delegate { MessageBox.Show("L1 Click"); });
    L2.Click += new RoutedEventHandler(delegate { MessageBox.Show("L2 Click"); });
}

When the Window loads up and everything is displayed, I click on the Button so that the parenting happens and events are added. I see L2 being moved as a child of L1, but when I click either, they don't respond to Click events. If I prevent the parenting form happening, they do respond to Click events. I am so confused as to why this is happening!
EDIT 2:
I replicated everything from the original post in a clean project, and it all works perfectly. So it's not a problem with the 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 with the following code added 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 is always triggered when ctrl-clicking on the 'dead' MenuItems. Their Click events continue to stop working after being parented. Still no solution nor indication of the problem...
EDIT 4:
I did the following test, adding the below code for both L1 and L2:
L1.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(delegate
{
    MessageBox.Show("L1 PreviewMouseLeftButtonDown"); 
});
// Same for L2

When Clicked, MenuItem L1 and L2 both respond to PreviewMouseLeftButtonDown, but continue to stop responding to Click after being parented.

A:

I've finally solved the mystery. I tracked down the problem to the parent Control stealing focus after any clicks are made within it, although it is weird that the menu stayed open and reacted to navigation even though it did not have focus. Everything seems to work with the call to Focus() being removed.
The details of what was happening:
WITHOUT MenuItem parenting:
1) I click on a root MenuItem and it responds to Click.
2) OnPreviewMouseDown is then intercepted by the parent and a call to Focus() is made.
There is no adverse results caused by the loss of focus in this situation since Click was successfully fired.
WITH MenuItem parenting:
1) I click on the menu and it opens.
2) OnPreviewMouseDown is then intercepted by the parent and a call to Focus() is made.
3) The menu stays open and continues to react to navigation (is this meant to happen??).
4) When I click the second time on any MenuItem to make a selection, the menu finally realises that it lost focus and closes - also discarding the click.
It then seems like a click was made and discarded by the MenuItem... leading to the problem experienced in the original post.

