Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm new in WPF and I have this :
HTML
<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>

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:
C#
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?
Posted
Updated 14-May-13 10:26am
v4

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.
 
Share this answer
 
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>


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
 
Share this answer
 

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