3

LongListSelector在一个页面中有这个:

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

这是EditVideo

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

        //Do Stuff

        this.RelodeTableData();
    }

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);
            }
        });
    }

问题是,当用户编辑一个项目并尝试编辑另一个项目之后,他得到了他编辑的最后一个项目selectedVideo

编辑后,我使用 ReloadTableData 刷新列表数据。

4

1 回答 1

11

好的,经过大量搜索如何解决这个问题。我发现如果我将Unload方法添加到ContextMenu,这将清除 DataContext。

private void ContextMenu_Unload(object sender, RoutedEventArgs e)
    {
        ContextMenu conmen = (sender as ContextMenu);
        conmen.ClearValue(FrameworkElement.DataContextProperty);
    }
于 2013-02-19T13:14:30.050 回答