0

我正在使用以下代码创建一个页面,并在用户长按时LongListSelector添加一个:ContextMenuListBoxItem

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

这是DeleteVideoFromHistory方法:

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

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

问题是,当 ai 在和 Item 上按下 Longpress 并单击其中的一个项目ContextMenu并在其他列表框中再次按下时,我得到最后一个 VideoItem 而不是我刚刚按下的当前。知道如何解决吗?

4

1 回答 1

1

我也遇到过这个问题,并通过清除 ContextMenu 的 Unload 上的 DataContext 来解决它。

    private void ContextMenu_Unload(object sender, RoutedEventArgs e)
    {
        ContextMenu conmen = (sender as ContextMenu);
        conmen.ClearValue(FrameworkElement.DataContextProperty);
    }
于 2013-07-19T09:42:07.640 回答