Search code examples
windows-phone-8contextmenu

ContextMenu selects old item


So basically this is the situation. I have a longlistselector that shows data (say, a list of cars):

    <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>

And then this is how I handle the deletion:

   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();
                    }
                }
        }

Now the issue is that after deleting few, the context menu starts to give old selection and does not update which makes var existing = db.Query<Feed>("select * from Cars where Id = " + data.Id.ToString()).FirstOrDefault(); return null obviously because it has been already deleted from database. Any idea how to fix this?


Solution

  • Probably this issue.

    private void ContextMenu_Unload(object sender, RoutedEventArgs e)
    {
        ContextMenu conmen = (sender as ContextMenu);
        conmen.ClearValue(FrameworkElement.DataContextProperty);
    }