sony
sony

Reputation: 1503

How to hide a menuitem if its submenu is collapsed?

enter image description hereI have a menu as below:

 <MenuItem Margin="10" x:Name="Products" Header="Products" FontFamily="Arial" VerticalAlignment="Center">
                <MenuItem x:Name="ProdCodes" Header="Product Codes" Click="ProdCodes_Click" Visibility="Collapsed" />
                <MenuItem x:Name="Formulations" Header="Formulation" Click="Formulation_Click" Visibility="Collapsed" />
                <MenuItem x:Name="ProdGroupText" Header="Group Text" Click="ProdGroupText_Click" Visibility="Collapsed" />                    
            </MenuItem>

At runtime, I check(in database) whether to display the submenu and display it accordingly. But there are times, where I have to hide all the submenus. In that case, I need to hide the menu(Products) as a whole. Is there a way to accomplish this?

Upvotes: 0

Views: 1282

Answers (2)

JesuX
JesuX

Reputation: 41

Maybe a bit last and if I understood it good...

If you are hot with Behaviors you can get it too... Personally I have a trigqer in style who set visibility of all roles (parents or leafs) of menuItems to collapsed when IsEnabled is false and my leafs item are linked with commands.

public class DisableMenuItemIfNoEnabledChildBehavior : Behavior<MenuItem>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.Command = new RelayCommand(
            p => { /*nothing*/ },
            p => AssociatedObject.Items.OfType<MenuItem>()
                    .Any(mi => (mi.Command != null && mi.Command.CanExecute(mi.CommandParameter)) || (mi.Command == null && mi.IsEnabled))
            );
    }

    protected override void OnDetaching()
    {
        AssociatedObject.Command = null;
        base.OnDetaching();
    }
}

And in xaml part:

<MenuItem Header="Parent MI"
          xmlns:b="clr-namespace:YourNsHere.Behaviors"
          xmlns:bh="http://schemas.microsoft.com/xaml/behaviors">
    <MenuItem.Icon>
        <Control Template="{StaticResource chat}" />
    </MenuItem.Icon>
    <bh:Interaction.Behaviors>
        <b:DisableMenuItemIfNoEnabledChildBehavior />
    </bh:Interaction.Behaviors>
    <MenuItem Command="{Binding MaybeCanExecute}"
              CommandParameter="{Binding}" 
              Header="Child MI 0">        
        <MenuItem.Icon>
            <Control />
        </MenuItem.Icon>
    </MenuItem>
    <MenuItem Header="Child MI 1"
              IsEnabled="False">
        <MenuItem.Icon>
            <Control />
        </MenuItem.Icon>
    </MenuItem>
</MenuItem>

Upvotes: 0

Christian
Christian

Reputation: 4375

Bind the Visibility property of your parent-menu using a converter. Inside the converter, check if all its sub-menu items are collapsed or not. Depending on that outcome, return "Collapsed" or "Visible".

In case you have not encountered converters yet, here is a tutorial on how to create one. http://www.switchonthecode.com/tutorials/wpf-tutorial-binding-converters

Upvotes: 1

Related Questions