0

I'm having a ListBox (binds to an observable collection) with the ListBoxItems design defined in the ItemsPanel StaticResource. The ItemsPanel is defined in the App.xml (like to have my stuff in a central place and the design for the ListBoxItem is used on different pages) Now for every Listbox I need to have a different ContextMenu, and there comes the thing, I don't get working...

In my App.xaml (small demo - didn't want to bloat the topic, here):

<Application.Resources>
    <DataTemplate x:Key="ListItemTemplate">
        <TextBlock Text="{Binding Title}" />
    </DataTemplate>
</Application.Resources>

In my MainPage.xaml I have the Listbox defined.:

<ListBox ItemsSource="{Binding Items}" 
         ItemTemplate="{StaticResource ListItemTemplate}">

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="toolkit:ContextMenuService.ContextMenu">
                <Setter.Value>
                    <toolkit:ContextMenu>
                        <toolkit:MenuItem Header="Test" />
                    </toolkit:ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

However, if I long-press a ListBoxItem, nothing happens. I have no errors - nothing - but the Contextmenu just doesn't show up... (neither in the emulator, nor on the device).

If I add a ContextMenu to the LayoutRoot gird or something else, It works like a charm.

m_c
  • 59
  • 1
  • 9

1 Answers1

1

You should try to put the ContextMenu in your ListItemTemplate

<Application.Resources>
<DataTemplate x:Key="ListItemTemplate">
    <StackPanel>
        <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu>
                <toolkit:MenuItem x:Name="DoneMenuItem" 
                    Header="Done"
                    Command="{Binding Main.DoneCommand, Source={StaticResource Locator}}"
                    CommandParameter="{Binding}"/>
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>
        <TextBlock Text="{Binding Title}" />
    </StackPanel>
</DataTemplate>

And you can remove your ListBox.ItemContainerStyle tag

Grimeh
  • 161
  • 1
  • 12
Fabrice
  • 921
  • 1
  • 6
  • 11
  • Hi Fabrice, thanks for the post. Can you explain, what the Command and CommandParameter does? Where does the binding go? – m_c Feb 18 '13 at 21:19
  • In fact in the MenuItem you can use the Click event if you don't use the MVVM pattern. With my code (and the MVVM pattern) I'll execute a command from my ViewModel and pass the selecteditem from the ListBox as a parameter for my command. Anyway, you can simply use the click event in the MenuItem. The binding is the same, it goes here: – Fabrice Feb 19 '13 at 08:21
  • Thanks. That was the missing thing. It's rather a large topic which needs some more reading. Basically I added now a class, containing my commands and added this as a static resource to have the code separated and not messing up my ViewModel. Gave me some headaches where the Binding would end up, but everything worked out fine :) – m_c Feb 19 '13 at 10:03