System.Windows.Controls.MenuItem without icon area

Maxim V. Pavlov picture Maxim V. Pavlov · Mar 11, 2011 · Viewed 20.5k times · Source

Whenever I try to play with the look of WPF menu item, I get a wast opportunities to customize the header, which is basically the text of an item.

What I need, is to have a black menu, with white text and no "icon area".

||Some text http://img848.imageshack.us/img848/7622/iconarea.png

How can I remove the icon area of the menu item?

Thanks.

Answer

Nitesh picture Nitesh · Jul 18, 2013

You can achieve it by defining ItemsPanel property of MenuItem.

Create an ItemsPanelTemplate resource

<ItemsPanelTemplate x:Key="MenuItemPanelTemplate">
    <StackPanel Margin="-20,0,0,0" Background="White"/>
</ItemsPanelTemplate>

Add below MenuItem style to resources and you are done.

<Style TargetType="{x:Type MenuItem}">
   <Setter Property="ItemsPanel" Value="{StaticResource MenuItemPanelTemplate}"/>
</Style>

To apply same Style to a ContextMenu, you need to create one more Style as following-

<Style TargetType="{x:Type ContextMenu}">
   <Setter Property="ItemsPanel" Value="{StaticResource MenuItemPanelTemplate}"/>
</Style>