LongListSelector和文本菜单返回错误的项目 [英] LongListSelector and ContextMenu return wrong item
本文介绍了LongListSelector和文本菜单返回错误的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的 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
编辑的最后一个项目。
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
.
我用ReloadTableData刷新列表数据之后,我编辑它。
I use the ReloadTableData to refresh the list data after i edit it.
推荐答案
好了很多搜索如何解决这件事情后。我发现用,如果我加入卸载
方法的文本菜单
,这是明确的DataContext的。
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);
}
这篇关于LongListSelector和文本菜单返回错误的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文