Home > Blockchain >  WPF : Remove context menu items based on condition
WPF : Remove context menu items based on condition

Time:11-08

I have a context menu in WPF with following items:

<ContextMenu x:Key="MyContextMenu">
        <MenuItem Header="{x:Static localization:Resources.MyContext_Command1}" Command="{Binding Command1}" />
        <MenuItem Header="{x:Static localization:Resources.MyContext_Command2}" Command="{Binding Command2}" />
        <Separator />
        <MenuItem Header="{x:Static localization:Resources.MyContext_Command3}" Command="{Binding Command3}" Visibility="{Binding IsItemActive, Converter={converters:BooleanToVisibilityConverter}}" />
        <MenuItem Header="{x:Static localization:Resources.MyContext_Command4}" Command="{Binding Command4}" Visibility="{Binding IsItemActive, Converter={converters:BooleanToVisibilityConverter}}" />
        <Separator Visibility="{Binding IsItemActive, Converter={converters:BooleanToVisibilityConverter}}"/>
</ContextMenu>

With the above code these menu items(Command3 and Command4) are appearing grey(Disabled) when IsItemActive = false and appear black(Enabled) when IsItemActive = true. But i want my Menuitems(Command3 and Command4) and also Seperator to Disappear / Appear from context menu based on "IsItemActive".How can i achieve this ?

CodePudding user response:

This should do the trick. But I cannot get my bindings to work now so it's not tested. You will have to give a name to the element that you are applying this in order to make the binding work. For my case, it's a window.

Create a dependency property for IsItemActive only then you can bind. Tip : type propdp and double tab in Visual Studio to use code snippet.

<Window x:Class.....
    x:Name="mainwindow" 
...>

Then in the <Window.Resources> define the styles

<Style x:Key="MenuItemStyle" TargetType="MenuItem">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=mainwindow,Path=IsItemActive}" Value="False">
                <Setter Property="Visibility" Value="Collapsed" />
            </DataTrigger>
            <DataTrigger Binding="{Binding ElementName=mainwindow,Path=IsItemActive}" Value="True">
                <Setter Property="Visibility" Value="Visible" />
            </DataTrigger>
        </Style.Triggers>
</Style>

Duplicate the style and change TargetType="Separator" Then apply the style on your context menu items and separator.

<ContextMenu x:Key="MyContextMenu">
    <MenuItem Header="{x:Static localization:Resources.MyContext_Command1}" Command="{Binding Command1}" />
    <MenuItem Header="{x:Static localization:Resources.MyContext_Command2}" Command="{Binding Command2}" />
    <Separator />
    <MenuItem Header="{x:Static localization:Resources.MyContext_Command3}" Command="{Binding Command3}" Style="{StaticResource MenuItemStyle}" />
    <MenuItem Header="{x:Static localization:Resources.MyContext_Command4}" Command="{Binding Command4}" Style="{StaticResource MenuItemStyle}" />
    <Separator Style="{StaticResource SeparatorStyle}"/>
</ContextMenu>
  • Related