Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place a dividing line in a menu in WPF

Tags:

wpf

xaml

menu

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> 
like image 877
Andrew Shepherd Avatar asked Aug 17 '09 22:08

Andrew Shepherd


People also ask

How do you add a line in WPF?

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.

What is separator in WPF?

A Separator control draws a line, horizontal or vertical, between items in controls, such as ListBox, Menu, and ToolBar.

How do I create a menu in XAML?

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.

Which XAML tag is used for menu bar?

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.


2 Answers

Use a Separator like this:

<MenuItem Header="_Open" Command="ApplicationCommands.Open" /> <Separator /> <MenuItem Header="E_xit" Command="ApplicationCommands.Close" /> 
like image 87
RichieHindle Avatar answered Oct 03 '22 05:10

RichieHindle


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.

like image 31
Marcin Avatar answered Oct 03 '22 03:10

Marcin