基本上就是这种情况。我有longlistselector
显示数据(例如,汽车列表):
<phone:LongListSelector x:Name="list" ItemsSource="{Binding CarList}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Click="DeleteMenuItem_Click" Header="delete"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<TextBlock Text="{Binding SomeText}">
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
这就是我处理删除的方式:
private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
{
Car data = (sender as MenuItem).DataContext as Car;
using (var db = new SQLiteConnection(App.DBpath))
{
var existing = db.Query<Feed>("select * from Cars where Id = " + data.Id.ToString()).FirstOrDefault();
if (existing != null)
{
db.RunInTransaction(() =>
{
db.Delete(existing);
});
App.ViewModel.loadCarData();
}
}
}
现在的问题是,在删除少数之后,上下文菜单开始给出旧选择并且不更新使var existing = db.Query<Feed>("select * from Cars where Id = " + data.Id.ToString()).FirstOrDefault();
显然返回null,因为它已经从数据库中删除了。知道如何解决这个问题吗?
答案 0 :(得分:2)
可能是this问题。
private void ContextMenu_Unload(object sender, RoutedEventArgs e)
{
ContextMenu conmen = (sender as ContextMenu);
conmen.ClearValue(FrameworkElement.DataContextProperty);
}