YosiFZ
YosiFZ

Reputation: 7900

LongListSelector and ContextMenu

I am using the following code to make a page with LongListSelector and add a ContextMenu when the user made a long press on ListBoxItem:

<Controls:LongListSelector Height="Auto" x:Name="historylist" HorizontalContentAlignment="Stretch"  
                                   Background="White" SelectionChanged="DidPressSelectItem" Hold="HoldListBox">
            <Controls:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <local:SearchTemplateSelector Content="{Binding}" HorizontalContentAlignment="Stretch">
                        <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu>
                                <toolkit:MenuItem Header="Remove from history" Click="DeleteVideoFromHistory"/>
                                <toolkit:MenuItem Header="Remove from cache" Click="DeleteVideoFromCache"/>
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>

                        <local:SearchTemplateSelector.VideoTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Rectangle Height="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Fill="Black" Opacity="0.3" />
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Image Margin="0" Source="{Binding Path=ImgUrl}" HorizontalAlignment="Left" Width="100" Height="100" Tag="{Binding idStr}"/>
                                        <Grid Grid.Column="1" Margin="10,0,8,0">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="60"/>
                                                <RowDefinition Height="*"/>
                                                <RowDefinition Height="*"/>
                                            </Grid.RowDefinitions>
                                            <TextBlock Text="{Binding Name}" FontSize="20" Foreground="Black" TextWrapping="Wrap" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                                            <StackPanel Orientation="Horizontal" Margin="0,-5,0,0" Grid.Row="1">
                                                <TextBlock Text="Views:  " FontSize="20" Foreground="Black"/>
                                                <TextBlock Text="{Binding ViewCount}" FontSize="20" Foreground="Black"/>
                                            </StackPanel>

                                            <Grid Grid.Row="2">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="*"/>
                                                    <ColumnDefinition Width="*"/>
                                                </Grid.ColumnDefinitions>

                                                <TextBlock Text="{Binding TimeStr}" FontSize="20" Foreground="Black" Margin="0,0,0,0" />
                                            </Grid>
                                        </Grid>
                                    </Grid>

                                </Grid>

                            </DataTemplate>
                        </local:SearchTemplateSelector.VideoTemplate>

                        <local:SearchTemplateSelector.VideoTemplateCached>
                            <DataTemplate>
                                <Grid>
                                    <Rectangle Height="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Fill="Black" Opacity="0.3" />
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Image Margin="0" Source="{Binding Path=ImgUrl}" HorizontalAlignment="Left" Width="100" Height="100" Tag="{Binding idStr}"/>
                                        <Grid Grid.Column="1" Margin="10,0,8,0">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="60"/>
                                                <RowDefinition Height="*"/>
                                                <RowDefinition Height="*"/>
                                            </Grid.RowDefinitions>
                                            <TextBlock Text="{Binding Name}" FontSize="20" Foreground="Black" TextWrapping="Wrap" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                                            <StackPanel Orientation="Horizontal" Margin="0,-5,0,0" Grid.Row="1">
                                                <TextBlock Text="Views:  " FontSize="20" Foreground="Black"/>
                                                <TextBlock Text="{Binding ViewCount}" FontSize="20" Foreground="Black"/>
                                            </StackPanel>

                                            <Grid Grid.Row="2">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="*"/>
                                                    <ColumnDefinition Width="*"/>
                                                </Grid.ColumnDefinitions>

                                                <TextBlock Text="{Binding TimeStr}" FontSize="20" Foreground="Black" Margin="0,0,0,0" />
                                                <TextBlock Text="Cached" FontSize="20" Foreground="Red" Margin="50,0,0,0" Grid.Column="1" />
                                            </Grid>
                                        </Grid>
                                    </Grid>

                                </Grid>

                            </DataTemplate>
                        </local:SearchTemplateSelector.VideoTemplateCached>

                    </local:SearchTemplateSelector>
                </DataTemplate>


            </Controls:LongListSelector.ItemTemplate>
        </Controls:LongListSelector>

And this is the DeleteVideoFromHistory method:

private void DeleteVideoFromHistory(object sender, RoutedEventArgs e)
    {
        VideoItem video = (sender as MenuItem).DataContext as VideoItem;
        if (video == null) { return; }

        historyRep.RemoveFromHistory(video);
        this.RelodeTableData();
    }

The issue is that when a i press a Longpress on and Item and click one of the items in the ContextMenu and the press again on other listbox i get last VideoItem and not the current i just pressed. Any idea how to fix it?

Upvotes: 0

Views: 706

Answers (1)

Mark Monster
Mark Monster

Reputation: 397

I've had this issues as well, and solved it by clearing the DataContext on the Unload of the ContextMenu.

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

Upvotes: 1

Related Questions