随手建了WPF窗口
<Window x:Class="WpfAppTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfAppTest" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition/> </Grid.RowDefinitions> <Menu x:Name="MenuControl" Background="Transparent" > </Menu> </Grid> </Window>
后台代码
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); List<MenuItem> menuItems = new List<MenuItem>(); MenuItem a = new MenuItem(); a.Header = "A"; a.Items.Add(new MenuItem { Header = "AAA" }); menuItems.Add(a); menuItems.ForEach(menuItem => MenuControl.Items.Add(menuItem)); } }
效果图
标签:菜单,MVVM,menuItems,menuItem,Add,MenuItem,new,WPF From: https://www.cnblogs.com/cvol/p/17307045.html