-
Sep 27th, 2010, 05:06 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] hiding WPF menu items based on a variable
wow, the winforms to WPF transition looks like its going to be fun. Does anybody know how to hide a submenu item in wpf based on a variable?
ie. in winforms style:
Code:
if administrator = false
administratormenuitem.visible=False
endif
thanks guys - as always, i'll keep looking for the solution on my own and if i find it i'll post it.....
-
Sep 28th, 2010, 12:56 AM
#2
Re: hiding WPF menu items based on a variable
It is now named Visibility.
Code:
administratormenuitem.Visibility = System.Windows.Visibility.Collapsed;
-
Sep 28th, 2010, 02:37 PM
#3
Thread Starter
Hyperactive Member
Re: hiding WPF menu items based on a variable
how would i choose a specific item? I have a menu in place called menu1 and on menu1 i have File, Tools, Help
Under tools i have admin, reports
i am trying to collapse or hide admin based on the above example (but still display 'reports' and also the tool menu itself) - but its not showing up under intellisense, i've tried menu1.items.item(1), etc etc
how would i get to the collection of admin and reports in the tools menu item under menu1 via code?
-
Sep 28th, 2010, 06:12 PM
#4
Re: hiding WPF menu items based on a variable
OK so your menu structure looks something like this right? (but with more menus than just the Tool menu)
Code:
<Menu Height="23" Name="Menu1" VerticalAlignment="Top">
<MenuItem Header="Tools">
<MenuItem Header="Admin" />
<MenuItem Header="Reports" />
</MenuItem>
</Menu>
I think all you need to do is cast the items to the correct type. In Winforms we know that an item in a Menu's "Items" collection has to be of type MenuItem (or whatever the Winforms equivalent is) but because WPF is so flexible, that is not the case in a WPF application... you could add whatever you wanted to the Items collection in a WPF Menu element (of course the vast majority of the time it will just be MenuItems). So we have to cast the items in the Items collection to the MenuItem type if we want to access their properties.
So for example, if we wanted to hide the Admin menu item using the example menu I've defined above, we can do this:
vb.net Code:
DirectCast(DirectCast(Menu1.Items(0), MenuItem).Items(0), MenuItem).Visibility = Windows.Visibility.Collapsed
Or to make it a little easier to understand:
vb.net Code:
Dim ToolsMenuItem As MenuItem
ToolsMenuItem = DirectCast(Menu1.Items(0), MenuItem) 'Cast the first item in Menu1's Items collection to a MenuItem type
Dim AdminMenuItem As MenuItem
AdminMenuItem = DirectCast(ToolsMenuItem.Items(0), MenuItem) 'Cast the first item in the Tools menu item's Items collection to a MenuItem type
AdminMenuItem.Visibility = Windows.Visibility.Collapsed 'Hide the Admin menu item
Hope that helps
-
Sep 28th, 2010, 09:20 PM
#5
Thread Starter
Hyperactive Member
Re: hiding WPF menu items based on a variable
absolutely 100% perfectly bang on - thank you sir it worked like a charm. The single largest thing for me to get rid of is assuming a winforms mentality in that if you create a menu, it creates menuitems and intellisense knows that those are the only thing to display in winforms code....however, not so in wpf - we could have a dancing bear icon if we like, we could have gradient text, we could plug in any number of items that aren't normally associated with menus - and that's the biggest thing the winforms guys like me need to understand - try to depart from linear thinking based on the item you just dragged on to the designer - its going to take me a while.......and i'll likely need several course corrections along the way.
I would highly recommend that a new forum be created specifically for winforms developers that need to see wpf equivalents if one doesn't exist already ("WPF for Winforms Developers" perhaps?)- these little items that are covered here make such a huge difference to the guys and gals that are writing code and trying to stay current with the latest radical change from our friends at Microsoft.
Thanks again - the experts in here are awesome.
Chris 128, thanks a million.....
-
Sep 28th, 2010, 09:39 PM
#6
Thread Starter
Hyperactive Member
Re: [RESOLVED] hiding WPF menu items based on a variable
here's one for elegance Chris128, instead of index number in the items(x) directcast code, can we take it a step further and use the collection entry name?
-
Sep 29th, 2010, 12:47 AM
#7
Re: [RESOLVED] hiding WPF menu items based on a variable
One method you can use is to just specify the Name of the MenuItem.
Code:
<MenuItem Command="Paste" Name="YourMenuItem" />
-
Sep 29th, 2010, 05:30 AM
#8
Thread Starter
Hyperactive Member
Re: [RESOLVED] hiding WPF menu items based on a variable
how about in the codebehind instead of the xaml?
-
Sep 29th, 2010, 06:02 AM
#9
Re: [RESOLVED] hiding WPF menu items based on a variable
Originally Posted by trevorjeaton
here's one for elegance Chris128, instead of index number in the items(x) directcast code, can we take it a step further and use the collection entry name?
Unfortunately there's no built in way to do that, again because of the fact that WPF doesn't know what could be in the Items collection so it can't index them by any particular property.
However, through the magic of Extension Methods, we can create our own method that 'attaches' itself to the Menu and MenuItem classes - the end result being that we can do something like this (which I think is very close to what you were after) :
vb.net Code:
Menu1.FindItemByName("Tools").FindItemByName("Admin").Visibility = Windows.Visibility.Collapsed
If you like the look of that and aren't familiar with Extension Methods, all you need to do is add a new Module to your project and then write the extension methods for both the Menu and MenuItem classes in the Module like so:
vb.net Code:
Module Extensions
<System.Runtime.CompilerServices.Extension()> _
Public Function FindItemByName(ByVal Source As Menu, ByVal Name As String) As MenuItem
For Each Item As MenuItem In Source.Items
If CStr(Item.Header) = Name Then
Return Item
End If
Next
Return Nothing
End Function
<System.Runtime.CompilerServices.Extension()> _
Public Function FindItemByName(ByVal Source As MenuItem, ByVal Name As String) As MenuItem
For Each Item As MenuItem In Source.Items
If CStr(Item.Header) = Name Then
Return Item
End If
Next
Return Nothing
End Function
End Module
Note that the module doesn't have to be called Extensions, that is just what I called mine.
If you want more info on extension methods then see here: http://msdn.microsoft.com/en-us/library/bb384936.aspx
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|