ContextMenu 的 MenuItem DataContext 返回旧项 [英] ContextMenu's MenuItem DataContext returns old items

查看:28
本文介绍了ContextMenu 的 MenuItem DataContext 返回旧项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 LongListSelector 中使用了 ContextMenu,以便我可以删除绑定到 LLS 的列表中的一些项目.

I am using a ContextMenu within a LongListSelector so that I can delete some items in the list bounded to the LLS.

我正在关注最近的指南 此处 以实现 LLS(尽管我没有使用 JumpList).我唯一改变的是让基组类扩展 ObservableCollection 而不是 List.

I am following a recent guide here in order to implement the LLS (though I am not using the JumpList). The only thing I've changed is to have the base group class extend ObservableCollection instead of List.

我遇到的问题是,一旦我实现了 ContextMenu 并从那里删除,我可以从可见列表中的同一位置"删除两次,然后它会崩溃.调试显示,在第二次删除后,MenuItem 的Datacontext 返回上一个被删除的项目.所以当我在列表中搜索它时,我得到的索引是-1.我可以抓住这个,但我不知道如何找出真正被选为项目的内容.

The issue I am having is that once I've implemented the ContextMenu and delete from there, I can delete from the same "location" in teh visible list twice and then it would crash. Debugging shows that after the second delete, the Datacontext of the MenuItem returns the previous item that was deleted. So when I search for it in the list, the index I get is -1. I can catch this but I don't know how to then find out what was really selected as the item.

我的 contextMenu 的 XAML 部分如下:

My XAML section for the contextMenu is as below:

<phone:LongListSelector.ItemTemplate>
    <DataTemplate>
        <Grid toolkit:TiltEffect.IsTiltEnabled="True">
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu  x:Name="conmen" Loaded="ContextMenu_Loaded">
                      <toolkit:MenuItem Header="Delete" Click="DeleteItem_Click"/>
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>

             <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="Auto"/>
                 <ColumnDefinition Width="*"/>
                 <ColumnDefinition Width="Auto"/>
             </Grid.ColumnDefinitions>

             <Border Grid.Column="0" Background="{StaticResource PhoneInverseBackgroundBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}">
                 <TextBlock Text="{Binding Usr, StringFormat='x{0}'}" FontSize="32" HorizontalAlignment="Left" Width="48"/>
             </Border>

             <Border Grid.Column="1" Background="{StaticResource PhoneInverseBackgroundBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}">
                 <TextBlock Text="{Binding Name}" FontSize="32" HorizontalAlignment="Left" />
             </Border>

             <Border Grid.Column="2" Background="{StaticResource PhoneInverseBackgroundBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}">
                <TextBlock Text="{Binding Type, StringFormat=\{0:C\}}" FontSize="32" HorizontalAlignment="Right" />
             </Border>
        </Grid>
    </DataTemplate>
</phone:LongListSelector.ItemTemplate>

这是我删除项目的 delete_click 函数的开始:

And this is the start of my delete_click function to remove the item:

    private void DeleteItem_Click(object sender, RoutedEventArgs e)
    {            
        var menItem = (MenuItem)sender;
        editCartItem = (Model.Cartitem)menItem.DataContext;

        cartIndex = editCartItem.Id;

        deleteIndex = this.cartList.FindIndex(FindItem);

两次删除后,(Model.Cartitem)menItem.DataContext 返回之前删除的项目.

After two deletes, the (Model.Cartitem)menItem.DataContext returns the previously deleted item.

我已经搜索了一段时间,并发现了几年前不同框架和场景的类似案例.我想知道在 WP8 中是否有更新的方法来执行此操作.

I have been searching for a while and have found similar cases for different frameworks and scenarios from some years before. I wanted to know if there was an updated method for doing this in WP8.

我看到有人建议使用 Loaded 或 Opened 事件手动重新分配 ContextMenu 的数据上下文,但 DataContext 仍然依赖于 LLS 中的特定项目.所以我不能仅仅将它的上下文指向 LLS.

I have seen suggestions in manually re-assigning the datacontext of the ContextMenu with a Loaded or Opened event, but the DataContext still relies on a specific item in the LLS. So I can't just point it's context to the LLS's.

我还看到有人指出它是一个带有补丁的错误 here看起来和我的问题完全一样,但我不知道如何应用补丁,或者它是否与我使用 WP8 的情况有关.

I've also seen that it's been pointed to as a bug with a patch here which seems exactly like my issue, but I had no idea on how to apply the patch or if it even pertained to my situation with WP8.

我还一直确保清除 LLS 的选定项目,并尝试在每次操作后重新分配它的 itemSource,但无济于事.

I've also been ensuring that the LLS's selected item is cleared and have tried re-assigning it's itemSource after each operation to no avail.

任何方向正确的帮助或建议都会很棒.我在这里看到了一些关于此的帖子,但我相信我已经通过了这些要点(例如简单地获取 menuItem 并使用 ObservableCollection ......).

Any help or advice in the right direction would be great. I've seen some posts on here about this, but I believe I've already passed those points (such as simply getting the menuItem and using an ObservableCollection...).

推荐答案

xaml:

<toolkit:ContextMenu Opened="ContextMenu_Opened">... </toolkit:ContextMenu>

c#

private void ContextMenu_Opened(object sender, RoutedEventArgs e)
{
    var menu = (ContextMenu)sender;
    var owner = (FrameworkElement)menu.Owner;
    if (owner.DataContext != menu.DataContext)
        menu.DataContext = owner.DataContext;
}

你可以看到:Windows Phone 工具包上下文菜单项在删除然后添加项时绑定了错误的对象

这篇关于ContextMenu 的 MenuItem DataContext 返回旧项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆