如何使用代码在 WPF 中创建菜单分隔栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/280115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:46:46  来源:igfitidea点击:

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

wpfvb.netmenu

提问by Lamar

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.

我正在使用 vb.net 以编程方式在 WPF 中创建菜单。有人可以告诉我如何在代码中向菜单添加分隔栏吗?请不要xaml。

回答by Jeff Donnici

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

WPF 有一个用于此目的的Separator控件,它还会在出现在工具栏上时分隔您的菜单项。来自 MSDN 文档:

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.

Separator 控件在控件中的项目(例如 ListBox、Menu 和 ToolBar)之间绘制一条水平线或垂直线。分隔符控件不响应任何键盘、鼠标、鼠标滚轮或平板电脑输入,并且无法启用或选择。

In code:

在代码中:

using System.Windows.Controls;

//

Menu myMenu = new Menu();
myMenu.Items.Add(new Separator());

回答by Adrian Toman

In xaml:

在 xaml 中:

<Menu>
   <MenuItem Header="Menu Item 1" />
   <Separator />
   <MenuItem Header="Menu Item 1" />
<Menu>