0

我有一个 ListPicker 说 listPicker1,其中使用隔离存储填充了项目,当我长按(按住)listPicker1 中的任何项目时,将出现一个上下文菜单,其中包含“删除”、“取消”等选项。现在当我点击“删除',我想从 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;
    }
4

3 回答 3

0

SaveNumber data = (sender as MenuItem).DataContext as SaveNumber;

于 2014-05-07T09:50:15.757 回答
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);
}
于 2013-02-21T07:25:37.737 回答
-1

为保持事件创建处理程序,然后使用其索引获取项目

listPicker1.SelectedIndex;

于 2013-04-18T12:26:28.817 回答