WPF中的Dinamic菜单。 [英] Dinamic menu in WPF.

查看:72
本文介绍了WPF中的Dinamic菜单。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Bindings和Observable集合从具有Bindings和Observable集合的数据库表中将菜单项添加到WPF中的菜单控件(而不是上下文菜单)?我有这个菜单:



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>





SubMenu1和_SuMenu2是数据库表中的值:



codSubMenu | SubMenuColum | CommandColumn

----------------------------------------- -----------------

1 | _SubMenu1 | SubMenu1Command

2 | _SubMenu2 | SubMenu2Command



我需要这样的东西:





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>





当我运行应用程序时,当我按下选项File和MyMenu时,菜单必须显示:



文件| MyMenu

退出| SubMenu1

| SubMenu2



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

File | MyMenu
Exit | SubMenu1
| SubMenu2

推荐答案

System.Windows.Controls.Menu System.Windows。 Controls.MenuItem 拥有属性 Items System.Windows.Controls.ItemsControl.Items 其方法为添加

http://msdn.microsoft.com/en-us/library/system.windows.controls.menu.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/system.windows.controls。 menuitem.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system。 windows.controls.itemcollection.add.aspx [ ^ ]。



我希望其余的都清楚。



-SA
Both System.Windows.Controls.Menu and System.Windows.Controls.MenuItem have the property Items, System.Windows.Controls.ItemsControl.Items which has the method Add:
http://msdn.microsoft.com/en-us/library/system.windows.controls.menu.aspx[^]
http://msdn.microsoft.com/en-us/library/system.windows.controls.menuitem.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items.aspx[^],
.http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcollection.add.aspx[^].

I hope the rest is clear.

—SA


我在XAML中没有快速解决方案。我需要从数据库中获取子菜单项,根据具体的配置文件,一些用户拥有其他只有2或3项的所有项目。独特的方法是在XAML中创建具有禁用项目的菜单,将菜单引用传递给ViewModel(如果是MVVM App)并与ObservableCollection进行比较,只启用项目equals:



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:

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





ViewModel:

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







我认为之前程序员得到了codeproject更好的支持和帮助。

谢谢。




I think that before the programmers had better support and help from codeproject.
Thanks.


这篇关于WPF中的Dinamic菜单。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆