Place a dividing line in a menu in WPF

WpfXamlMenu

Wpf Problem Overview


In XAML, how do I put a standard dividing line in a menu?

eg

<MenuItem Header="_File" Name="m_fileMenu">
    <MenuItem Header="_Open" Command="ApplicationCommands.Open"/>
    <!-- Trying to put a divider here! -->
    <MenuItem Header="-" />  <!-- Wrong guess -->
    <MenuItem Header="E_xit" Command="ApplicationCommands.Close" />
</MenuItem>

Wpf Solutions


Solution 1 - Wpf

Use a Separator like this:

<MenuItem Header="_Open" Command="ApplicationCommands.Open" />
<Separator />
<MenuItem Header="E_xit" Command="ApplicationCommands.Close" />

Solution 2 - Wpf

I needed to iterate through MenuItems for various reasons, and using Separator meant a bit of casting, so I used a 1px high MenuItem instead

<MenuItem Height="1" Background="LightGray"/>

The correct answer most definitely is to use Separator, but the above works visually too, and can be a solution in some cases.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew ShepherdView Question on Stackoverflow
Solution 1 - WpfRichieHindleView Answer on Stackoverflow
Solution 2 - WpfMarcinView Answer on Stackoverflow