1

我正在为 windows phone 7 开发一个照片应用程序。我有一个列表框,其中所有捕获和保存的图片都在包装面板中。在 Listbox SelectionChanged 事件中,上下文菜单可以通过点击并按住正常工作,但我希望上下文菜单仅出现在点击/点击事件上,没有点击并按住。

当我在列表框之外使用这种机制时,它可以工作,但为什么这不适用于列表框项目。

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 (这是上下文菜单的名称)中给出错误,意味着它没有显示在这里。任何帮助请。

4

1 回答 1

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;
    }
}
于 2013-09-20T06:44:08.247 回答