Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a menu separator bar in WPF using code

Tags:

vb.net

wpf

menu

I am creating menus in WPF programatically using vb.net. Can someone show me how I can add separator bar to a menu in code? No xaml please.

like image 453
Lamar Avatar asked Nov 11 '08 05:11

Lamar


People also ask

How to create menu bar in WPF application?

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.

What is a Separator bar and How is It created?

Separator bars insert a line between menu items and items on a toolbar. You can use separator bars to indicate groupings within the menu list or toolbar, or simply to provide a visual break in a list. To add a separator bar to a menu: Add a menu item as described in Adding, Inserting, and Deleting Menu Items.

Which .NET WPF class used to create menus?

WPF Menu control is represented by the Menu class in C#. This menus tutorial and menus code examples explain how to use menus in WPF using C#. In WPF, the Menu and the MenuItem classes represent a menu and a menu item respectively. A Menu is a collection of menu items with a command associated with each menu item.


2 Answers

WPF has a Separator control for just that purpose and it also separates your menu items when the appear on a toolbar. From the MSDN docs:

A Separator control draws a line, horizontal or vertical, between items in controls, such as ListBox, Menu, and ToolBar. Separator controls do not react to any keyboard, mouse, mouse wheel, or tablet input and cannot be enabled or selected.

In code:

using System.Windows.Controls;

//

Menu myMenu = new Menu();
myMenu.Items.Add(new Separator());
like image 177
Jeff Donnici Avatar answered Oct 12 '22 18:10

Jeff Donnici


In xaml:

<Menu>
   <MenuItem Header="Menu Item 1" />
   <Separator />
   <MenuItem Header="Menu Item 1" />
<Menu>
like image 44
Adrian Toman Avatar answered Oct 12 '22 17:10

Adrian Toman