【问题标题】:Context menu of each Listbox item on tap even instead of tap & hold wp7甚至不是点击并按住 wp7,而是点击每个列表框项目的上下文菜单
【发布时间】:2013-09-20 06:19:03
【问题描述】:

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

【问题讨论】:

    标签: windows-phone-7


    【解决方案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;
        }
    }
    

    【讨论】:

    • 非常感谢 Umar,它工作正常。但是抛出了一个空异常,请解决它。
    • 我检查了各个方面,甚至将点击事件代码放在 Try/Catch 块中,但仍然抛出 NullException。请帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2013-08-12
    • 2012-09-26
    相关资源
    最近更新 更多