Domanda

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.

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top