获取listpiker wp7中长按项的索引

时间:2013-02-21 06:27:10

标签: c# windows-phone-7

我有一个ListPicker说listPicker1,其中填充了使用隔离存储的项目,当我长按(Hold)listPicker1中的任何项目时,上下文菜单ll会出现“删除”,“取消”等选项。现在当我点击'删除',我想从lisPicker1删除该项目,我无法做到,因为我没有获得DeleteEvent Handler中的长按项目的索引或实际数据(即使我曾尝试使用GestureListener_Hold,但是徒劳) 。以下是我试过的代码。如果可以,PL会帮助我。提前谢谢。

<toolkit:ListPicker SelectionChanged="listPicker1_SelectionChanged"   FontSize="24"  ItemCountThreshold="1" Name="listPicker1"  Margin="6,0,102,513" ListPickerMode="Full" Height="72" VerticalAlignment="Bottom" >           
            <toolkit:ListPicker.ItemTemplate >
                <DataTemplate x:Name="PickerFullModeItemTemplate">
                    <StackPanel x:Name="sp" Orientation="Horizontal" >
                       <TextBlock x:Name="txt" Text="{Binding ContactName}" Margin="16 0 0 0" FontSize="35" />                  
                        <TextBlock  Name="number" Text="{Binding ContactNumber}" Foreground="Azure" FontSize="28"/>
                          <toolkit:ContextMenuService.ContextMenu >
                            <toolkit:ContextMenu>
                                <toolkit:MenuItem Header="Delete" Click="MenuItem_Click" />
                                <toolkit:MenuItem Header="Cancel" />
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                    </StackPanel>
                </DataTemplate>
            </toolkit:ListPicker.ItemTemplate>
        </toolkit:ListPicker>
     }

   private void MenuItem_Click(object sender, RoutedEventArgs e)
    {

        SaveNumber data = (sender as ListPicker).SelectedItem as SaveNumber; //nullRef exception here//
        if (MessageBox.Show("Are you sure you want to delete the contact from list?", "Delete Contact", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
        {       
           XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.Indent = true;
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("People.xml", FileMode.Create))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<SaveNumber>));
                    using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                    {                            
                        serializer.Serialize(xmlWriter, DeletePersonData(data));
                        this.listPicker1.ItemsSource = data2;
                    }
                }
            }
        }
    }
    private List<SaveNumber> DeletePersonData(SaveNumber ob)
    {   
        //data2.RemoveAt(listPicker1.SelectedIndex); //selected index is always set to 1//

        this.data2.Remove(ob);              
        return data2;
    }

3 个答案:

答案 0 :(得分:0)

您需要找到长按项目的根,然后从此处获取容器中此项目的索引。使用LinqToVisualTree

private void MenuItem_Click(object sender, RoutedEventArgs e)
{

  StackPanel rootItem = e.OriginalSource.Ancestors()
                                        .OfType<StackPanel>()
                                        .Where(i => i.Name == "sp");

  int index = listPicker1.ItemContainerGenerator.IndexFromContainer(rootItem);
}

答案 1 :(得分:0)

SaveNumber data =(发送者为MenuItem).DataContext为SaveNumber;

答案 2 :(得分:-1)

为hold事件创建一个处理程序,然后使用其索引

获取该项目

listPicker1.SelectedIndex;