Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to add menu items to menu control (not contextmenu) in WPF from a database table with Bindings and Observable collections?. I have this menu:

<Menu HorizontalAlignment="Left" Height="27" VerticalAlignment="Top" Width="649">
            <MenuItem Header="_File">
                <MenuItem Header="_Exit" Command="{Binding ExitCommand}"/>
            </MenuItem>
            <MenuItem Header="_MyMenu">
                <MenuItem Header="_SubMenu1" Command="{Binding  SubMenu1Command}" />
                <MenuItem Header="_SubMenu2" Command="{Binding  SubMenu2Command}" />
            </MenuItem>
        </Menu>


The "SubMenu1" and "_SuMenu2" are values from the database table:

codSubMenu | SubMenuColum | CommandColumn
----------------------------------------------------------
1 | _SubMenu1 | SubMenu1Command
2 | _SubMenu2 | SubMenu2Command

I need something this:

<Menu HorizontalAlignment="Left" Height="27" VerticalAlignment="Top" Width="649"
	ItemsSource="{Binding ObservableCollectionMenu}">
            <MenuItem Header="_File">
                <MenuItem Header="_Exit" Command="{Binding ExitCommand}"/>
            </MenuItem>
            <MenuItem Header="_MyMenu">
                <MenuItem Header="{Binding  ObservableCollectionMenu.SubMenuColumn}" 
			Command="{Binding  ObservableCollectionMenu.CommandColumn}" />
            </MenuItem>
        </Menu>


When I run the app the menu must show this when I press the options File and MyMenu:

File | MyMenu
Exit | SubMenu1
| SubMenu2
Posted
Updated 7-Oct-13 11:33am
v3
Comments
Sergey Alexandrovich Kryukov 2-Oct-13 19:08pm    
Did you try just to read MSDN documentation?
—SA
Ejrr1085 7-Oct-13 17:14pm    
The code that I show in mky question say MSDN documentation.
Sergey Alexandrovich Kryukov 7-Oct-13 17:53pm    
What's your problem then?
—SA

 
Share this answer
 
Comments
Ejrr1085 7-Oct-13 17:21pm    
You sugest a listbox like menu items, I did this. I need a drop-down menu, I'm working with WPF and MVVM.
Sergey Alexandrovich Kryukov 7-Oct-13 17:53pm    
For WPF, this is exactly the same thing. All menus available in WPF part of FCL use the same menu item class, System.Windows.Controls.MenuItem.
—SA
I don't have a quick solution in XAML. I needed get submenus items from database, according specific profiles, some users had all items others only 2 or 3 items. The unique way is create the menu in XAML with disabled items, pass the menu reference to ViewModel(if is MVVM App) and compare with the ObservableCollection, only the items equals are enabled:

HTML
<menu horizontalalignment="Left" height="27" verticalalignment="Top" width="649" name="menu1">
      <menuitem header="_File">
          <menuitem header="_Exit" command="{Binding ExitCommand}" />
      </menuitem>
      <menuitem header="_MyMenu">
          <menuitem header="_SubMenu1" command="{Binding  Command1}" isenabled="False" />
          <menuitem header="_SubMenu2" command="{Binding  Command2}" isenabled="False" />
      </menuitem>
</menu>


ViewModel:

C#
for (int i = 0; i < ObservableCollectionMenu.Count; i++)
{

    for (int j = 0; j < ((MenuItem)menu1.Items[1]).Items.Count; j++)
    {
         if (((MenuItem)((MenuItem)menu1.Items[1]).Items[j]).Header.ToString().Equals(ObservableCollectionMenu[i].SubMenuColumn))
         {
            ((MenuItem)((MenuItem)menu1.Items[1]).Items[j]).IsEnabled = true;
             break;
         }
    }
}



I think that before the programmers had better support and help from codeproject.
Thanks.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900