Amr Ibrahim
Amr Ibrahim

Reputation: 177

WPF dynamic menuItems

Trying to add some MenuItems dynamically to a pre-defined <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);
    }
}  

and here is the result :

enter image description here

I have no clue what the problem is. Any Help!

UPDATE

The Code compiles fine and gives me the result in the picture above.

Note :

I created a new project and tested the code. and it works fine!!

Upvotes: 0

Views: 1693

Answers (2)

user7583356
user7583356

Reputation: 76

In your mainwindow.xaml, you can have something similar to this...

<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>

Then in mainwindow.xaml.cs, you can have something like this...

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; }
}

Upvotes: 0

I.B
I.B

Reputation: 2923

You did everything right, it's just you're declaring your MenuItem mItem1 but when using it you're writing mItem (you forgot the 1 at the end) same thing for the second one.

This is how the code should be :

                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);

Upvotes: 1

Related Questions