Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LongListSelector and ContextMenu return wrong item

I have this LongListSelector in a 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 is the 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 the 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 the user edit a item and try to edit another item after that he get the last item he edit in selectedVideo.

I use the ReloadTableData to refresh the list data after i edit it.

like image 697
YosiFZ Avatar asked Jun 09 '26 16:06

YosiFZ


1 Answers

Ok after a lot of searching how to fix this thing. i fount that if i add Unload method to the ContextMenu and this is clear the DataContext.

private void ContextMenu_Unload(object sender, RoutedEventArgs e)
    {
        ContextMenu conmen = (sender as ContextMenu);
        conmen.ClearValue(FrameworkElement.DataContextProperty);
    }
like image 90
YosiFZ Avatar answered Jun 11 '26 06:06

YosiFZ