简体   繁体   English

LongListSelector和ContextMenu

[英]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 : 当用户在ListBoxItem上长LongListSelector ,我使用以下代码创建一个带有LongListSelector的页面并添加一个ContextMenu

<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: 这是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();
    }

The issue is that when ai 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. 问题是,当ai长按on和Item并在ContextMenu单击其中一个项目,然后再次按其他列表框时,我得到了最后一个VideoItem,而不是我刚刚按下的当前项目。 Any idea how to fix it? 知道如何解决吗?

I've had this issues as well, and solved it by clearing the DataContext on the Unload of the ContextMenu. 我也遇到了这个问题,并通过清除ContextMenu的Unload上的DataContext解决了它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM