WPF动态menuItems

时间:2017-03-09 21:35:01

标签: c# wpf xaml

尝试将一些MenuItems动态添加到预定义的<Menu>

XAML

<Window x:Class="FSBEM.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:FSBEM"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:src="clr-namespace:FSBEM.Models"
    mc:Ignorable="d"
    Title="BEM" Height="471.997" Width="1186.374" 
    WindowStartupLocation="CenterScreen" 
    WindowState="Maximized" 
    Closing="Window_Closing"
    Icon="/Media/Icons/AppL.ico">

    <Grid Name="MainGrid" FlowDirection="LeftToRight">
       <Menu Name="MainMenu" HorizontalAlignment="Stretch" Height="23  VerticalAlignment="Top" >
         <MenuItem Name="M_1" Header="1">
              <MenuItem Name="M_2" Header="2"/>
              <MenuItem Name="M_3" Header="3" />
         </MenuItem>
         <MenuItem Name="M_Test" Header="Test"/>
      </Menu>
   </Grid>
</Window>

Code-Behind

public partial class MainWindow : Window
{
    public MainWindow()
    {
         InitializeComponent();

         MenuItem mItem1 = new MenuItem();
         mItem1.Name = "MenuItem1";
         mItem1.Header = "MenuItem1";
         M_Test.Items.Add(mItem1);

         MenuItem mItem2 = new MenuItem();
         mItem2.Name = "MenuItem2";
         mItem2.Header = "MenuItem2";
         M_Test.Items.Add(mItem2);
    }
}  

以下是结果:

enter image description here

我不知道问题是什么。任何帮助!

更新

“守则”编辑得很好,并在上图中给出了结果。

Note

我创建了一个新项目并测试了代码。它工作正常!!

2 个答案:

答案 0 :(得分:1)

你做的一切都是正确的,只是你正在宣布你的MenuItem mItem1,但是当你使用它时,你正在为第二个创建mItem(你忘记了1)。

代码应该是这样的:

                MenuItem mItem1 = new MenuItem();
            mItem1.Name = "MenuItem1";
            mItem1.Header = "MenuItem1";
            M_Test.Items.Add(mItem1);

            MenuItem mItem2 = new MenuItem();
            mItem2.Name = "MenuItem2";
            mItem2.Header = "MenuItem2";
            M_Test.Items.Add(mItem2);

答案 1 :(得分:0)

在你的mainwindow.xaml中,你可以有类似的东西......

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Name="mainwindow">
    <Window.Resources>
        <CollectionViewSource Source="{Binding ElementName=mainwindow, Path=Windows}" x:Key="YourMenuItems"/>
     </Window.Resources>

    <Grid DataContext="{Binding ElementName=mainwindow}">
        <Menu Height="24" VerticalAlignment="Top">
        <MenuItem Header="_View" >
                <MenuItem Header="Windows">
                    <MenuItem.ItemsSource>
                        <CompositeCollection>
                            <CollectionContainer Collection="{Binding Source={StaticResource MenuItems}}" />                            
                        </CompositeCollection>
                    </MenuItem.ItemsSource>
                    <MenuItem.ItemContainerStyle>
                        <Style>
                            <Setter Property="MenuItem.Header" Value="{Binding MenuItemName}"/>
                        </Style>
                    </MenuItem.ItemContainerStyle>
                </MenuItem>
            </MenuItem>
        </Menu>
    </Grid>
</Window>

然后在mainwindow.xaml.cs中,你可以有这样的东西......

public partial class MainWindow : Window
{
    private ObservableCollection<MenuItem> _menuItems= new ObservableCollection<YourObj>();

    public MainWindow()
    {
        InitializeComponent();
        MenuItems.Add(new MenuItem{ Title = "Menu Item 1" });
        MenuItems.Add(new MenuItem{ Title = "Menu Item 2" });
    }

    public ObservableCollection<MenuItem> MenuItems
    {
        get { return _menuItems; }
        set { _menuItems= value; }
    }
}

public class YourObj
{
    public string MenuItemName{ get; set; }
}