1

Which WPF control allows listing of items and context menus on each of the items? Up until now, I've been using a ListBox, but it seems as though there's no easy way of adding context menus to its items. Instead, I'm looking at changing the ListBox to another control that does allow context menus.

The only ListBox properties/methods I'm using right now are "SelectedIndex" and "SelectedItem". As long as the suggested control supports this or has another substitute, I should be fine.

Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
rafale
  • 1,704
  • 6
  • 29
  • 43
  • can you post your XAML? `ListBoxItem` has a `ContextMenu` property so it should work fine – kenwarner Aug 12 '11 at 06:40
  • @qntmfred : See Meleak's post for the XAML I'm using. My previous code probably wasn't implemented properly. – rafale Aug 13 '11 at 06:17

3 Answers3

4

To add a ContextMenu to ListBoxItem you use the ItemContainerStyle

<ListBox ItemsSource="{Binding ...}">
    <ListBox.Resources>
        <ContextMenu x:Key="listBoxItemContextMenu">
            <MenuItem Header="{Binding YourProperty}" />
        </ContextMenu>
    </ListBox.Resources>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="ContextMenu"
                    Value="{StaticResource listBoxItemContextMenu}"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <!--...-->
</ListBox>
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
1

You can use the DataGrid control in wpf for listing. Also you can add contextMenu like

<DataGrid AutoGenerateColumns="False" Height="200"  Width="200" >
        <DataGrid.ContextMenu>
        <ContextMenu >
            <MenuItem Header="Menu Header" Click="MenuItem_Click"  />
        </ContextMenu>
        </DataGrid.ContextMenu>
</DataGrid>

Also refer Create contextmenus for datagrid rows

Community
  • 1
  • 1
Shebin
  • 3,310
  • 2
  • 24
  • 27
0

This example doesn't use any binding or DataTemplates like you might typically see in a real app, but hopefully it helps

    <ListBox>
        <ListBox.Items>
            <ListBoxItem Content="Item A">
                <ListBoxItem.ContextMenu>
                    <ContextMenu>Delete A</ContextMenu>
                </ListBoxItem.ContextMenu>
            </ListBoxItem>
            <ListBoxItem Content="Item B">
                <ListBoxItem.ContextMenu>
                    <ContextMenu>Delete B</ContextMenu>
                </ListBoxItem.ContextMenu>
            </ListBoxItem>
            <ListBoxItem Content="Item C">
                <ListBoxItem.ContextMenu>
                    <ContextMenu>Delete A</ContextMenu>
                </ListBoxItem.ContextMenu>
            </ListBoxItem>
        </ListBox.Items>
    </ListBox>
kenwarner
  • 28,650
  • 28
  • 130
  • 173
  • So in my app I'd have to programatically add those context menus to each item in the list, one by one. Isn't there a better way around this? Perhaps there's another control besides the ListBox? – rafale Aug 12 '11 at 07:16