Windows Phone 长按ListBox出菜单,获取Item内容,以及Item的Index

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

                <ListBox Height="607" HorizontalAlignment="Left" Margin="-4,0,0,0" Name="listBox1" VerticalAlignment="Top" Width="460" SelectionChanged="listBox1_SelectionChanged" Tap="listBox1_Tap">

                  <toolkit:GestureService.GestureListener>

                        <toolkit:GestureListener Hold="ListItem_Hold"/>

                    </toolkit:GestureService.GestureListener>

                    <toolkit:ContextMenuService.ContextMenu>

                        <toolkit:ContextMenu

                                    Width="200"

                                    BorderBrush="Red"

                                    BorderThickness="5" Background="Chocolate">

                            <toolkit:MenuItem Header="编辑帖子" Click="MenuItemFirst_Click"/>

                            <toolkit:MenuItem Header="删除帖子" />

                            <toolkit:Separator/>

                            <toolkit:MenuItem Header="查看资料"  />

                        </toolkit:ContextMenu>

                    </toolkit:ContextMenuService.ContextMenu> 

            </ListBox>

        </Grid>

    </Grid>
private void ListItem_Hold(object sender, Microsoft.Phone.Controls.GestureEventArgs e)

{

     //第一种实现方法(获取到的zhi+1)

   IEnumerable<UIElement> hits = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(LayoutRoot), listBox1);

    ListBoxItem item;

    int index = -1;

    foreach (UIElement element in hits)

    {

         if (element is ListBoxItem)

         {

             item = (ListBoxItem)element;

             index =  listBox1.Items.IndexOf(item.Content);

         }

    }

    MessageBox.Show("Currently hovering over item index " + index.ToString());

}

 

你可能感兴趣的:(windows phone)