Steph M
Steph M

Reputation: 31

Defining another level of menu items within a MenuItem ItemTemplate

I have a list of objects on my WPF window, all having a context menu which allows the user to copy or move items into different panels (so the ItemsSource of the Context menu is this list of panels, and "Copy" and "Move" are sub-menuitems). However, I have a "CanCopy" property defined in the object that determines whether or not the object can actually be copied. How can I show/hide this MenuItem depending on the value of this property? My problem seems to be in defining this variable additional level of MenuItems.

At first I tried something like this, but obviously it's not quite what I'm looking for, since this doubles up the PanelName MenuItem in to two MenuItem containers:

<MenuItem Header="Panels..." ItemsSource="{Binding PanelsList}">
    <MenuItem.ItemTemplate>
        <DataTemplate>
            <MenuItem Header="{Binding PanelName}">
                <MenuItem Header="Copy" Visibility="{Binding CanCopy,Converter={StaticResource BoolToHiddenConverter}}"/>
                <MenuItem Header="Move"/>
            </MenuItem>
        </DataTemplate>
    </MenuItem.ItemTemplate>
</MenuItem>

Suggestions?

Upvotes: 1

Views: 292

Answers (1)

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

Add as Content not ItemTemplate

<MenuItem Header="Panels..." ItemsSource="{Binding PanelsList}">
     <MenuItem Header="{Binding PanelName}">
         <MenuItem Header="Copy" Visibility="{Binding CanCopy,Converter={StaticResource BoolToHiddenConverter}}"/>
         <MenuItem Header="Move"/>
     </MenuItem>
</MenuItem>

Upvotes: 0

Related Questions