0

所以基本上情况就是这样。我有一个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 显然是因为它已经从数据库中删除了。知道如何解决这个问题吗?

4

1 回答 1

2

大概是这个问题。

private void ContextMenu_Unload(object sender, RoutedEventArgs e)
{
    ContextMenu conmen = (sender as ContextMenu);
    conmen.ClearValue(FrameworkElement.DataContextProperty);
}
于 2013-10-07T13:14:00.070 回答