0
votes

I want to add the context menu to ListBox in WP7, I modify the data template as following below:

<ListBox Name="lbx1" Margin="10,0,10,0" Height="435"  ItemContainerStyle="{StaticResource ListBoxItemStyle1}" FontSize="40" Tap="lbx1_Tap" >
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <toolkit:ContextMenuService.ContextMenu>
                                        <toolkit:ContextMenu>
                                            <toolkit:MenuItem Header="Copy" Click="ListBoxGeneralCopy_Click"/>
                                        </toolkit:ContextMenu>
                                    </toolkit:ContextMenuService.ContextMenu>
                                    <TextBlock Text="{Binding .}" />
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

I can only cause tap and hold event to show the contextmenu above the text, the non-text region could not cause the event, I really confused. Does someone can help me?

2

2 Answers

0
votes

Your textbloc will only occupy so much space as necessary. Therefore your stackpanel will also only be as wide as necessary.

You can set the HorizontalAlignment-value of both to stretch so it occupies the whole width:

<ListBox Name="lbx1" Margin="10,0,10,0" Height="435"  ItemContainerStyle="{StaticResource ListBoxItemStyle1}" FontSize="40" Tap="lbx1_Tap" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel HorizontalAlignment="Stretch">
                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu>
                        <toolkit:MenuItem Header="Copy" Click="ListBoxGeneralCopy_Click"/>
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
                <TextBlock Text="{Binding .}"  HorizontalAlignment="Stretch"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
0
votes

Set StackPanel background property. When background is transparent cannot work, and hold event is fired only in non-transparent region of stackpanel such as text-regions.