WPF XAML defined MenuItem reuse starts working, then disappears

Viewed 587

The following simple code attempts to reuse a MenuItem defined in the Window.Resources on two separate Menus.

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
  <Window.Resources>
    <collections:ArrayList x:Key="menuItemValues">
       <MenuItem Header="First"/>
       <MenuItem Header="Second"/>
       <MenuItem Header="Third"/>
    </collections:ArrayList>
    <MenuItem x:Key="menuItem" x:Shared="False"
              ItemsSource="{StaticResource menuItemValues}"
              Header="Shared menu item"/>
  </Window.Resources>
  <StackPanel>
    <Menu HorizontalAlignment="Left" VerticalAlignment="Top">
      <StaticResource ResourceKey="menuItem"/>
      <StaticResource ResourceKey="menuItem"/>
    </Menu>
  </StackPanel>
</Window>

This starts out great and when you first select the menus, all looks well. The first menu has the desired MenuItems,

enter image description here

So does the second:

enter image description here

But when you navigate back to the first menu, the MenuItems disappear:

enter image description here

Can someone explain why the menu disappears and a way to get this to work?

This was discovered while investigating another SO question that was getting an exception. I tried to use a strategy discussed on another SO question and it seemed to solve the problem until you navigate back to the menu a second time and it disappears.

I have reproduced this issue on 2 separate machines:

  1. Win 10, VS2013 Ult V12.0.40629.00 Update 5, .NET V4.6.0138
  2. Win 7, VS2013 Prem V12.0.31101.00 Update 4, .NET V4.5.51209
1 Answers
Related