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.
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.
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.
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.
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());
In xaml:
<Menu>
<MenuItem Header="Menu Item 1" />
<Separator />
<MenuItem Header="Menu Item 1" />
<Menu>
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