如何在窗口加载时获取WPF菜单项名称? [英] How to get WPF Menu items names when window loaded?
问题描述
我是WPF的新手,我有这个:
I''m new in WPF and I have this :
<Grid>
<Menu VerticalAlignment="Top" Margin="8,13,20,0" Foreground="#FF4B5D51" Height="29" Name="baseMenu"
HorizontalAlignment="Stretch" removed="{x:Null}" BorderBrush="#FF4B5D51"
HorizontalContentAlignment="Right" FontFamily="Tahoma" FontSize="14">
<MenuItem Visibility="Collapsed" Header="Management" x:Name="mnuManagement" Foreground="#FFE3A036"
FontSize="15">
<!--Employees-->
<MenuItem Visibility="Collapsed" Header="Employees" x:Name="EmpsMenuItem" removed="{x:Null}"
FontSize="15" Foreground="#FFE3A036" BorderBrush="{x:Null}">
</MenuItem>
<MenuItem Visibility="Collapsed" Header="Categories" x:Name="catMenuItem" removed="{x:Null}"
FontSize="15" Foreground="#FFE3A036" BorderBrush="{x:Null}">
</MenuItem>
<MenuItem Visibility="Collapsed" Header="Products" x:Name="prodMenuItem" removed="{x:Null}"
FontSize="15" Foreground="#FFE3A036" BorderBrush="{x:Null}">
</MenuItem>
</MenuItem>
<!--الموردين-->
<MenuItem Visibility="Collapsed" Header="Suppliers" x:Name="mnuSuppliers" Foreground="#FFE3A036"
FontSize="15">
<!--الموردين-->
<MenuItem Visibility="Collapsed" Header="Branches" x:Name="BranchesMenuItem" removed="{x:Null}"
FontSize="15" Foreground="#FFE3A036" BorderBrush="{x:Null}">
</MenuItem>
</MenuItem>
</Menu>
</Grid>
我想要做的是获取每个菜单项名称并将它们放在一个数组中,我想这样做是为了根据菜单项名称给予用户权限as:如果ai有用户号码。 1&我会给他这个许可:
What I want to do is get every menu item name and put them in an array, I want to do that to give the user permission according to menu item name such as: If a i have user no. 1 & I will give him this permission:
Management.isenable = true;
EmpsMenuItem.isenable = false;
mnuSuppliers.isenable = true;
所以我想循环到这个数组给他权限,但我不知道如何做到这一点。
我该怎么做?
So I want to looping into this array to give him permissions ,but I don''t have any idea about how to do that.
How would I do this?
推荐答案
你可以采取各种方法来实现这一目标。
可以在form.controls集合上进行foreach循环,在那里搜索MenuItem类型的控件,然后从中收集name属性并放入在数组中。然后,当你想要检索一个控件时,你可以使用这样的扩展方法:
http://stackoverflow.com/questions/8737091/get-control-by-name-including-children [ ^ ]
来检索控件,然后设置相应的启用/禁用。
另一种方法可能是使用命令来启用/禁用菜单:< br $> b $ b
http://www.skimedic.com/blog/post/2008/07/14/WPF-Menus-and-Commands.aspx [ ^ ]
希望这些想法可以帮到你。
There are various approaches you could take to achieve this.
one could be to do a foreach loop on the form.controls collection where you are searching for a control of type MenuItem, then collect the name property from it and put that in an array. Then when you want to retrieve a control you could use an extension method such as this:
http://stackoverflow.com/questions/8737091/get-control-by-name-including-children[^]
to retrieve the control, and in turn set the appropriate enable/disable.
Another approach could be to use commands to enable/disable menus:
http://www.skimedic.com/blog/post/2008/07/14/WPF-Menus-and-Commands.aspx[^]
Hope these ideas help you.
我ave实际上没有对此进行过测试,但它应该适用于你的简单案例:
I have not actually tested this, but it should work for your simple case:
var names = baseMenu.Items.Cast<menuitem>().Select(j => j.Name);</menuitem>
我必须做的是根据所选记录动态更改上下文菜单。我使用 ContextMenuOpening
事件,然后使用ViewModel中的方法来确定 MenuItem
是否应该可见
What I have had to do is dynamically change a context menu depending on the selected record. I used the ContextMenuOpening
event and then used a method in the ViewModel to determine if MenuItem
should be visible
这篇关于如何在窗口加载时获取WPF菜单项名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!