Results 1 to 9 of 9

Thread: [RESOLVED] hiding WPF menu items based on a variable

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Resolved [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.....

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: hiding WPF menu items based on a variable

    It is now named Visibility.
    Code:
     administratormenuitem.Visibility = System.Windows.Visibility.Collapsed;
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    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?

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. 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:
    1. Dim ToolsMenuItem As MenuItem
    2. ToolsMenuItem = DirectCast(Menu1.Items(0), MenuItem) 'Cast the first item in Menu1's Items collection to a MenuItem type
    3.  
    4. Dim AdminMenuItem As MenuItem
    5. AdminMenuItem = DirectCast(ToolsMenuItem.Items(0), MenuItem) 'Cast the first item in the Tools menu item's Items collection to a MenuItem type
    6.  
    7. AdminMenuItem.Visibility = Windows.Visibility.Collapsed 'Hide the Admin menu item

    Hope that helps
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Re: hiding WPF menu items based on a variable

    absolutely 100&#37; 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.....

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    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?

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    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" />
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Re: [RESOLVED] hiding WPF menu items based on a variable

    how about in the codebehind instead of the xaml?

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] hiding WPF menu items based on a variable

    Quote Originally Posted by trevorjeaton View Post
    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:
    1. 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:
    1. Module Extensions
    2.  
    3.     <System.Runtime.CompilerServices.Extension()> _
    4.     Public Function FindItemByName(ByVal Source As Menu, ByVal Name As String) As MenuItem
    5.         For Each Item As MenuItem In Source.Items
    6.             If CStr(Item.Header) = Name Then
    7.                 Return Item
    8.             End If
    9.         Next
    10.         Return Nothing
    11.     End Function
    12.  
    13.     <System.Runtime.CompilerServices.Extension()> _
    14.     Public Function FindItemByName(ByVal Source As MenuItem, ByVal Name As String) As MenuItem
    15.         For Each Item As MenuItem In Source.Items
    16.             If CStr(Item.Header) = Name Then
    17.                 Return Item
    18.             End If
    19.         Next
    20.         Return Nothing
    21.     End Function
    22.  
    23. 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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width