4
votes

I have some ContextMenu With some menuItems. One of the menuItems is "Add Item...". when the user will selects this item, he should see submenu with list of available items;

here is description of my contextMenu:

  • Add Item
    • Item 1
    • Item 2
    • Item 3
    • Item 4
  • Delete
  • Copy

the ItemsSource of 'Add Item' menuItem is binded to some observable collection. Each model in the collecion has 'Name' and 'IsEnabled' (I have converter which convert bool to visibility). Everything works fine except the fact that the items which has 'IsEnable' = false, are not visible, but I can see there space. for example: let say that Item 3 has IsEnable = false:

  • Add Item

    • Item 1
    • Item 2

    • Item 4

My bool2Vis converter return 'Collapsed' in case of false value.

Whay I'm doing wrong?

Here is the ContextMenu Code:

<ContextMenu  x:Key="mainContextMenu" DataContext="{Binding Source={x:Static fw:UIMainManager.Instance},Path=layoutManager}">
        <MenuItem Header="Add Item" Name="addItemMenu" ItemsSource="{Binding ControlBoxItems}" >
            <MenuItem.ItemTemplate>
                <DataTemplate>
                    <MenuItem Header="{Binding ControlName}"  Visibility="{Binding IsEnabled,Converter={StaticResource boolToVisibilityConverter}}"
                                  Command="{Binding Source={x:Static fw:ApplicationCommands.AddControlToScene}}" CommandParameter="{Binding}"/>
                </DataTemplate>
            </MenuItem.ItemTemplate>
        </MenuItem>
        <MenuItem Header="Copy" Command="{Binding Source={x:Static fw:ApplicationCommands.Copy}}" />
        <MenuItem Header="Paste" Command="{Binding Source={x:Static fw:ApplicationCommands.Paste}}" />
        <MenuItem Header="Cut"  Command="{Binding Source={x:Static fw:ApplicationCommands.Cut}}" />
        <MenuItem Header="Duplicated" Command="{Binding Source={x:Static fw:ApplicationCommands.DuplicateControl}}" />
        <MenuItem Header="Delete" Command="{Binding Source={x:Static fw:ApplicationCommands.DeleteControl}}" />
    </ContextMenu>
1

1 Answers

1
votes

I think the MenuItem is collapsed but not the ItemContainer of MenuItem. Try this:

<MenuItem Header="Add Item" Name="addItemMenu" ItemsSource="{Binding ControlBoxItems}">
    <MenuItem.ItemContainerStyle>
        <Style>
            <Setter Property="Visibility" Value="{Binding IsEnabled, Converter={StaticResource boolToVisibilityConverter}}" />
        </Style>
    </MenuItem.ItemContainerStyle>
    <MenuItem.ItemTemplate>
        <DataTemplate>
            <MenuItem Header="{Binding ControlName}"
                      Command="{Binding Source={x:Static fw:ApplicationCommands.AddControlToScene}}"
                      CommandParameter="{Binding}" />
        </DataTemplate>
    </MenuItem.ItemTemplate>
</MenuItem>