我有一个 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;
}