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>
To draw a line, create a Line element. Use its X1 and Y1 properties to set its start point; and use its X2 and Y2 properties to set its end point. Finally, set its Stroke and StrokeThickness because a line without a stroke is invisible. Setting the Fill element for a line has no effect, because a line has no interior.
A Separator control draws a line, horizontal or vertical, between items in controls, such as ListBox, Menu, and ToolBar.
Creating a WPF Menu at Design TimeThe Menu element in XAML creates a menu control. The Name property defines the name of the menu and Height and Width represents the height and width of a menu control. To position a menu control in a Window, the Margin, HorizontalAlignment and VerticalAlignment properties may be used.
The <Menu> and <MenuItem> XAML elements are used to create menus in XAML. Learn how to use menu and menu item in a WPF app. A menu control is a group of menu items.
Use a Separator
like this:
<MenuItem Header="_Open" Command="ApplicationCommands.Open" /> <Separator /> <MenuItem Header="E_xit" Command="ApplicationCommands.Close" />
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With