C#: Listbox Contextmenu for Listboxitems (WPF)

14,227

I would set the ContextMenu in the ListBoxItem's style, rather than in the DataTemplate:

<ListBox Name="simpleListBox"
         ItemsSource="{Binding SimpleList}"
         DisplayMemberPath="Name">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        ...
                    </ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
Share:
14,227
user437899
Author by

user437899

Updated on June 04, 2022

Comments

  • user437899
    user437899 almost 2 years

    i want for my Listbox in WPF a contextmenu. I did it with a contextmenu for the whole listbox, but you can richt-click to get the contextmenu even if you don't click on a item.

    I found something at google, but this didn't work out.

    I tried something like this:

    <ListBox Margin="5" ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}">
                    <TextBlock.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="{Binding Name}" Click="MenuItemName_Click"/>
                            <MenuItem Header="{Binding Capital}"  Click="MenuItemCapital_Click"/>
                            <MenuItem Header="{Binding Population}" Click="MenuItemPopulation_Click"/>
                        </ContextMenu>
                    </TextBlock.ContextMenu>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    I tried it with a textblock like in the example, with other elements like in other tutorials, i tired it without and many other things- but nothing worked. No contextmenu for my listbox items :(

    later i tried something like this:

     <ListBox.ItemTemplate>
         <DataTemplate>
             <ListBoxItem>
                 <ListBoxItem.ContextMenu>
                     <ContextMenu>
                         <MenuItem/>
                     </ContextMenu>
                 </ListBoxItem.ContextMenu>
             </ListBoxItem>
         </DataTemplate>
     </ListBox.ItemTemplate>
    

    But it didn't work too.

    Can someone give me a hint/working example :)?

    thank you

  • SubmarineX
    SubmarineX over 10 years
    @Thomas, I binding the Command with a RelayCommand in MenuItem, but the RalayCommand never is called.
  • Thomas Levesque
    Thomas Levesque over 10 years
    @SubmarineX, can you see any binding errors in the output window? If you see something like "Cannot find governing FrameworkElement...", have a look at this solution
  • SubmarineX
    SubmarineX over 10 years
    @ThomasLevesque thank u, now I have got it. Because the datacontext of the ListBoxItem is not the datacontext of the ListBox, so I binding the command to ListBoxItem's datacontext, then through messager notify the listbox's datacontext. And later I will see your solution.