ContextMenu MenuItem DataContext returns old items

I am using ContextMenu in LongListSelector so that I can remove some items in a list limited to LLS.

I am following a recent guide here to implement LLS (although I am not using JumpList). The only thing I changed is to force the base group class to extend the ObservableCollection instead of List.

The problem I am facing is that after I deployed ContextMenu and remove from there, I can remove from the same โ€œlocationโ€ as a visible list twice, and then it will work. Debugging shows that after the second removal, the Datacontext from MenuItem returns the previous item that was deleted. So when I look for it in the list, the index I get is -1. I can catch this, but I donโ€™t know how then to find out what is really selected as the element.

My XAML section for the Menu context looks like this:

<phone:LongListSelector.ItemTemplate> <DataTemplate> <Grid toolkit:TiltEffect.IsTiltEnabled="True"> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu x:Name="conmen" Loaded="ContextMenu_Loaded"> <toolkit:MenuItem Header="Delete" Click="DeleteItem_Click"/> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Border Grid.Column="0" Background="{StaticResource PhoneInverseBackgroundBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}"> <TextBlock Text="{Binding Usr, StringFormat='x{0}'}" FontSize="32" HorizontalAlignment="Left" Width="48"/> </Border> <Border Grid.Column="1" Background="{StaticResource PhoneInverseBackgroundBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}"> <TextBlock Text="{Binding Name}" FontSize="32" HorizontalAlignment="Left" /> </Border> <Border Grid.Column="2" Background="{StaticResource PhoneInverseBackgroundBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}"> <TextBlock Text="{Binding Type, StringFormat=\{0:C\}}" FontSize="32" HorizontalAlignment="Right" /> </Border> </Grid> </DataTemplate> </phone:LongListSelector.ItemTemplate> 

And this is the beginning of my delete_click function to delete an element:

  private void DeleteItem_Click(object sender, RoutedEventArgs e) { var menItem = (MenuItem)sender; editCartItem = (Model.Cartitem)menItem.DataContext; cartIndex = editCartItem.Id; deleteIndex = this.cartList.FindIndex(FindItem); 

After two deletions, the file (Model.Cartitem) menItem.DataContext returns the previously deleted item.

I searched for a while and found similar cases for different frameworks and scripts a few years before. I wanted to know if there was an updated method for this in WP8.

I saw suggestions for manually reassigning the datacontext ContextMenu with the Loaded or Opened event, but the DataContext still relies on a specific element in LLS. Therefore, I cannot just point its context to LLS.

I also saw that it was flagged as a bug with the patch here , which seems to be similar to my problem, but I had no idea how to apply the patch, or even if it is related to my situation with WP8.

I also ensured that the selected LLS element was cleared and tried to reassign its itemSource after each operation to no avail.

Any help or advice in the right direction would be great. I saw some posts here about this, but I believe that I have already gone through these points (for example, just get the menu item and use ObservableCollection ...).

+7
source share
2 answers

XAML:

 <toolkit:ContextMenu Opened="ContextMenu_Opened">... </toolkit:ContextMenu> 

FROM#

 private void ContextMenu_Opened(object sender, RoutedEventArgs e) { var menu = (ContextMenu)sender; var owner = (FrameworkElement)menu.Owner; if (owner.DataContext != menu.DataContext) menu.DataContext = owner.DataContext; } 

you can see: The context menu of the Windows Phone Toolkit Elements have the wrong object attached to them when the element is deleted and then added

+2
source

I just came across a simulation:

When adding items to the list, the datacontext of a menu item belonging to the newly added items is not set correctly.

The workaround that I completed was to rebuild the list after adding the item:

  MyListBox.ItemsSource = null; MyListBox.Items.Clear( ); MyListBox.ItemsSource = theList; 

Not sure if you were also working on your problem ...
You should also consider the performance impact for lists with many items.

0
source

All Articles