There is a MenuItem , which is filled with other MenuItem items using the Binding . Fragment of the XAML file:

 <MenuItem x:Name="FileCommands" Header="{DynamicResource menu_File}" ItemsSource="{Binding Path=Commands}"> <MenuItem.ItemContainerStyle> <Style TargetType="{x:Type MenuItem}"> <Setter Property="InputGestureText" Value="{Binding InputGestureText}" /> <Setter Property="Command" Value="{Binding Path=Command}" /> </Style> </MenuItem.ItemContainerStyle> <MenuItem.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Path=Commands}"> <MenuItem Header="{Binding Path=DisplayName}" /> </HierarchicalDataTemplate> </MenuItem.ItemTemplate> 

In this version of the menu is displayed as follows:

Incorrect display of MenuItem

When compared with the correct display of the menu, the difference is obvious:

Correct Display MenuItem

As you can see, if the MenuItem not displayed correctly, MenuItem element has two selection areas, internal and external, and the Command only works when you click on the external selection area. However, if I change an item in the HierarchicalDataTemplate from the MenuItem to the TextBlock like this:

 <HierarchicalDataTemplate ItemsSource="{Binding Path=Commands}"> <TextBlock Text="{Binding Path=DisplayName}" /> </HierarchicalDataTemplate> 

, the menu is displayed correctly, but with one feature. It seems to me that because it is not MenuItem that is used, TextBlock ignores underscores, which are used for quick access to menu items while the Alt key is held down and the text is displayed as is. It looks like this:

Correct display of MenuItem with TextBlock

I think this is not the only difference TextBlock from the MenuItem and there may be some other pitfalls that can be encountered in the future.

The question is: is it possible to display the MenuItem correctly, or will you have to sacrifice the ability to quickly access the menu items while holding the Alt key (if this is of course the only difference TextBlock from the MenuItem )?

  • @vitidev Can I use a HierarchicalDataTemplate with your specified solution? If yes, could you give us an example of use? If that doesn't bother you, of course. - XelaNimed
  • Difficult. I don’t know about the hierarchy and can’t check anything. I remembered that when I was struggling with this double display (but in the context menu, without accelerators and without nesting), I rummaged through the bookmarks and gave a link. However, I’ll add that ContextMenu and MenuItem have one strange property UsesItemContainerTemplate and if you look at stackoverflow.com/questions/20030750/… (look at the xaml code in the answer), then I understand that a hierarchical menu is being made - vitidev

1 answer 1

Try using Label or just AccessText instead of TextBlock , there accelerators are supported.

(And you have lost the dots above Ö in Öffnen.)


About the "incorrect display": your HierarchicalDataTemplate defines the content for the automatically created child MenuItem . If this content is another MenuItem , you just get two MenuItem 's, one on the other. Of course, this does not look right.

  • I really do not know how to thank you! It turned out when using AccessText . - XelaNimed
  • @XelaNimed: That's great, what happened! - VladD