Mattias K

I have a context menu with menu items. Some of the menu items have children (i.e. subgroups). The context menu is adapting it's content depending on what I right-click. This is accomplished because I have attached a command to each menu item that isn't a group, and these items become disabled when the command can't run.

Since I does't want disabled items in a context menu, I have a style making all disabled menu items take the visibility property "Collapsed", making them disappear when they become disabled.

The problem now is how do I hide menu item groups when all their children are collapsed



Re: Windows Presentation Foundation (WPF) Problem with adaptive context menus

Ben Carter - MSFT

There is no one "right" way to accomplish what you want to do. Here are some possibilities:

  • You could set the Visibility property manually when the menu opens. You can use the Opening event for that.
  • You can setup a mulitibinding with all of the children to tie the parent's Visibility to the children's. You'd have to do this in code and would need to account for the children changing. Also, if the objects in your Items collection are not MenuItems, then be sure to use the ItemContainerGenerator to map from your items to the MenuItem containers.
  • You could try to listen for changes to the Visibility property on MenuItems by registering a change callback and then updating the parent's Visibility property.

Ben






Re: Windows Presentation Foundation (WPF) Problem with adaptive context menus

Mattias K

Thanks, I'll give your solutions a try.