每个列表框项目的上下文菜单甚至可以点击而不是点击&持有wp7

时间:2013-09-20 06:19:03

标签: windows-phone-7

我正在为Windows Phone 7开发一个Photo应用程序。我有一个列表框,其中所有捕获和保存的图片都在包装面板内。在列表框SelectionChanged事件上,通过点按并保持上下文菜单工作正常,但我希望上下文菜单只出现在点击和暂停的点按/点击事件中。

当我在Listbox之外使用这样的机制时,它可以工作,但为什么这对列表框项不起作用。

XAML:

<ListBox x:Name="listBox" Height="701" Margin="0,49,0,6" SelectionChanged="lbSChange">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    <Setter Property="Margin" Value="0,4,0,4"/>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemsPanel >
                <ItemsPanelTemplate >
                    <toolkit:WrapPanel FlowDirection="LeftToRight" ItemWidth="110" ItemHeight="110" />

                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>

                <DataTemplate >

                    <ListBoxItem>

                    <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu x:Name="menu1" VerticalOffset="10.0"   IsZoomEnabled="True" Background="LightBlue" BorderBrush="Yellow" BorderThickness="3">

                                <toolkit:MenuItem Header="Email" />
                                <toolkit:MenuItem Header="Save"  />
                                <toolkit:Separator />
                                <toolkit:MenuItem Background="Green" Header="Delete item"    />

                                <toolkit:ContextMenu.ItemContainerStyle>

                                    <Style TargetType="toolkit:MenuItem">
                                        <Setter Property="Background" Value="Yellow" />
                                        <Setter Property="Margin" Value="5" />
                                    </Style>
                                </toolkit:ContextMenu.ItemContainerStyle>
                            </toolkit:ContextMenu>
                                    </toolkit:ContextMenuService.ContextMenu>

                        <StackPanel Orientation="Horizontal" Height="132" 
                        MouseLeftButtonDown="Tap_LeftButtonDown"
                        MouseLeftButtonUp="Tap_LeftButtonUp"
                        MouseLeave="Tap_MouseLeave">


                            <Image HorizontalAlignment="Left" 
                        Margin="6,6,0,0" 
                        x:Name="image1" 
                        Stretch="Uniform" 
                        VerticalAlignment="Top" Source="{Binding}"/>
                        </StackPanel>
                     </ListBoxItem>
                </DataTemplate>

            </ListBox.ItemTemplate>
        </ListBox>
   <Grid>
    <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener Tap="GestureListener_Tap" />
    </toolkit:GestureService.GestureListener>
    </Grid>

代码背后:

private void GestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
    {

        if (this.menu1.Parent == null)
        {
           this.menu1.IsOpen = true;
        }
    }

问题是:在GestureListener_Tap menu1(这是上下文菜单的名称)给出错误,意味着它没有在这里显示。任何帮助PLZ。

1 个答案:

答案 0 :(得分:1)

从项目模板中删除上下文菜单并将其放在列表框中,如下所示

<ListBox x:Name="listBox" Height="701" Margin="0,49,0,6" SelectionChanged="lbSChange">
        <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu x:Name="menu1" VerticalOffset="10.0"   IsZoomEnabled="True" Background="LightBlue" BorderBrush="Yellow" BorderThickness="3">

                            <toolkit:MenuItem Header="Email" />
                            <toolkit:MenuItem Header="Save"  />
                            <toolkit:Separator />
                            <toolkit:MenuItem Background="Green" Header="Delete item"    />

                            <toolkit:ContextMenu.ItemContainerStyle>

                                <Style TargetType="toolkit:MenuItem">
                                    <Setter Property="Background" Value="Yellow" />
                                    <Setter Property="Margin" Value="5" />
                                </Style>
                            </toolkit:ContextMenu.ItemContainerStyle>
                        </toolkit:ContextMenu>
                                </toolkit:ContextMenuService.ContextMenu>

        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="Margin" Value="0,4,0,4"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemsPanel >
            <ItemsPanelTemplate >
                <toolkit:WrapPanel FlowDirection="LeftToRight" ItemWidth="110" ItemHeight="110" />

            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>

            <DataTemplate >

                <ListBoxItem>


                    <StackPanel Orientation="Horizontal" Height="132" 
                    MouseLeftButtonDown="Tap_LeftButtonDown"
                    MouseLeftButtonUp="Tap_LeftButtonUp"
                    MouseLeave="Tap_MouseLeave">


                        <Image HorizontalAlignment="Left" 
                    Margin="6,6,0,0" 
                    x:Name="image1" 
                    Stretch="Uniform" 
                    VerticalAlignment="Top" Source="{Binding}"/>
                    </StackPanel>
                 </ListBoxItem>
            </DataTemplate>

        </ListBox.ItemTemplate>
    </ListBox>

                       

背后的代码

private void GestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
{

    if (this.menu1.Parent == null)
    {
       this.menu1.IsOpen = true;
    }
}