LongListSelector and ContextMenu return wrong item
I have LongListSelector
on the page:
<Controls:LongListSelector Height="Auto" x:Name="historylist" HorizontalContentAlignment="Stretch"
Background="Black" SelectionChanged="DidPressSelectItem">
<Controls:LongListSelector.ItemTemplate>
<DataTemplate>
<local:SearchTemplateSelector Content="{Binding}" HorizontalContentAlignment="Stretch">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Opened="ContextMenu_Opened">
<toolkit:MenuItem Header="Edit" Click="EditVideo"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
.
.
.
.
</local:SearchTemplateSelector>
</DataTemplate>
</Controls:LongListSelector.ItemTemplate>
And this EditVideo
private void EditVideo(object sender, RoutedEventArgs e)
{
VideoItem selectedVideo = (sender as MenuItem).DataContext as VideoItem;
if (video == null) { return; }
//Do Stuff
this.RelodeTableData();
}
And RelodeTableData
:
private void RelodeTableData()
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
searchResults.Clear();
for (int i = 0; i < historyRep.historyArray.Count; i++)
{
VideoItem item = historyRep.historyArray[i];
searchResults.Add(item);
}
});
}
And the problem is that when a user edits an item and tries to edit another item after that, he gets the last item he edits in selectedVideo
.
I am using ReloadTableData to update the data of a list after editing it.
+3
source to share
1 answer
Ok after a lot of searching how to fix this thing. i fount if i add a Unload
method to ContextMenu
and it's clear DataContext.
private void ContextMenu_Unload(object sender, RoutedEventArgs e)
{
ContextMenu conmen = (sender as ContextMenu);
conmen.ClearValue(FrameworkElement.DataContextProperty);
}
+11
source to share