简体   繁体   中英

How do I create a Menu dynamically instead of hard coded in the XAML?

I have successfully created a drop-down list in the XAML using a

.XAML
    <Grid>
        <DockPanel>
            <Menu>
                <MenuItem Header="Menu">
                    <MenuItem Header="{Binding Path=ddlXferData1}" Command="{Binding Path=XferData}" Click="MenuItem_Click" IsCheckable="True"/>
                    <MenuItem Header="{Binding Path=ddlXferData2}" Command="{Binding Path=XferData}" Click="MenuItem_Click" IsCheckable="True"/>
                    <MenuItem Header="{Binding Path=ddlXferData3}" Command="{Binding Path=XferData}" Click="MenuItem_Click" IsCheckable="True"/>
                    <Separator />
                    <MenuItem Header="{Binding Path=ddlSkillData1}" Command="{Binding Path=SkillData}" Click="MenuItem_Click" IsCheckable="True"/>
                    <MenuItem Header="{Binding Path=ddlSkillData2}" Command="{Binding Path=SkillData}" Click="MenuItem_Click" IsCheckable="True"/>
                    <MenuItem Header="{Binding Path=ddlSkillData3}" Command="{Binding Path=SkillData}" Click="MenuItem_Click" IsCheckable="True"/>
                </MenuItem>
            </Menu>

        </DockPanel>
    </Grid>

.xaml.cs
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mnItem = (MenuItem) e.OriginalSource;
            this.Model.menuItemSelected = (string) mnItem.Header;
        }

From here the Commands XferData and SkillData are used to reference the event handler method that basically does something depending on the this.Model.menuItemSelected

All of this is working properly and the way I need it to work.

However, now I need the Menu (drop-down list) options to be build or created dynamically.

I am not 100% sure what is the best course of action to design and implement a dynamic drop-down list:

Should I put a in the .xaml

<Button Name="MenuButton" Content="{Binding Path=btnMenu}" Command="{Binding Path=Menu}" Click="button1_Click" .../>

and in the .xaml.cs build the Menu with options

 private void button1_Click(object sender, RoutedEventArgs e)
    {
        // Make the main menu.
        Menu mainMenu = new Menu();
        DockPanel dockPanel = new DockPanel();

        dockPanel.Children.Add(mainMenu);
        mainMenu.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
        mainMenu.VerticalAlignment = System.Windows.VerticalAlignment.Top;

        // Make the menu items.
        MenuItem menuItem = new MenuItem();
        .....
    }

or

Should I dynamically build the XAML?

it's not recommended but you can do it like this :

//assuming you have Menu named MainMenu and you want to add Menu Item to it and Sub menu item to the item which you have just added
      MenuItem MainItem = new MenuItem();
      MainItem .Header = "Main Item";
      this.MainMenu.Items.Add(MainItem);

      MenuItem SubItem= new MenuItem();
      SubItem.Header = "Sub Item";
      SubItem.Click += new RoutedEventHandler(SubItem_Click);
      MainItem.Items.Add(SubItem);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM