【问题标题】:ContextMenu in DataTemplate Binding issueDataTemplate 绑定问题中的 ContextMenu
【发布时间】:2013-01-21 18:01:01
【问题描述】:

我在 LongListSelector 中有一个上下文菜单。 此列表在运行时创建和更新。

<phone:PanoramaItem Header="{Binding Path=LocalizedResources.SavedGamesHeader, Source={StaticResource LocalizedStrings}}" Orientation="Horizontal">
            <phone:LongListSelector Margin="0,0,-22,2" ItemsSource="{Binding SavedGames}">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical" Margin="12,2,0,20" Width="432">
                            <toolkit:ContextMenuService.ContextMenu>
                                <toolkit:ContextMenu>
                                    <toolkit:MenuItem Header="Remove" Click="RemoveSave_OnClick"/>
                                </toolkit:ContextMenu>
                            </toolkit:ContextMenuService.ContextMenu>
                            <Image Margin="10,5,10,0"  Height="173" Width="248" Source="{Binding Screen}" Stretch="Fill" HorizontalAlignment="Left"></Image>
                            <StackPanel Width="311" Margin="8,5,0,0" HorizontalAlignment="Left">
                                <TextBlock Tap="Save_OnTap" Tag="{Binding SavedGame}" Text="{Binding SaveName}" TextWrapping="Wrap" Margin="10,0" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeMedium}" Foreground="White" FontWeight="Bold" FontFamily="Arial Black" HorizontalAlignment="Left" />
                                <TextBlock Text="{Binding GameName}" TextWrapping="Wrap" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" HorizontalAlignment="Left" />
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                                    <TextBlock Text="Created on:" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />
                                    <TextBlock Text="{Binding Created}" TextWrapping="Wrap" Margin="5,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />
                                </StackPanel>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </phone:PanoramaItem>

这里是处理菜单项点击事件的方法

private void RemoveSave_OnClick(object sender, RoutedEventArgs e)
    {
        var menuItem = (MenuItem)sender;
        var saveViewModel = menuItem.DataContext as SavesViewModel;
        EmuStorageMgr.Instance.DeleteSave(saveViewModel.SavedGame.SaveFolder);
        App.ViewModel.RescanSaves();
    }

以下方法填充 SavedGames 列表

public ObservableCollection<SavesViewModel> SavedGames { get; private set; }
public void RescanSaves()
    {
        SavedGames.Clear();
        var saves = EmuStorageMgr.Instance.GetSaves();
        foreach (var save in saves)
        {
            SavedGames.Add(new SavesViewModel(save));
        }
        this.IsSavesLoaded = true;
        NotifyPropertyChanged("SavedGames");
    }

因此,第一次填充 SavedGames 集合时,它运行良好,但是当集合发生变化(删除一些旧项目,添加新项目)时,我观察到一些奇怪的行为。当 OnClick 事件被触发时,我看到 menuItem.DataContext 不是针对我单击的菜单项,而是针对一些已删除的旧菜单项。

【问题讨论】:

  • 当您的收藏发生变化时,您的菜单项也会发生变化吗?我认为这可能与绑定的完成方式和一侧没有更新有关。
  • 例如,RemoveSave_OnClick 可以删除项目,每次它调用 RescanSaves 都会清除集合并重新填充它

标签: c# silverlight-4.0 windows-phone-8 silverlight-toolkit


【解决方案1】:

我不能对你的帖子发表评论,所以我会在这里说:

这是一个已知问题,我也遇到过。我还没有找到任何方法来完全解决这个问题,也没有看到任何最近的解决方案。您可以查看我的帖子here以确保问题与您的一致。

到目前为止我看到的唯一解决方案在msdn blog from '11 中进行了描述。它确定了 Silverlight 框架中的问题,他提供了我实施的解决方法。在您的项目中包含类文件并使用 XAML 标记,它将允许您的上下文菜单与父级的数据上下文保持同步。我使用它时遇到了一个小副作用,所以它只是一个创可贴。

我还发现另一个论坛告诉我这是一个没有解决方案的已知问题,但可以在codeplex here 找到补丁。我对补丁的问题是我不知道如何实现它,而且 LLS(这是我使用 ContextMenu 的)直接迁移到 SDK 中,所以我被卡住了。

这就是我对这个问题的全部挖掘,希望对您有所帮助。如果还有其他人要添加,请添加。

更新: 使用上面提供的链接中的一些内容,我认为我有一个更好的解决方案。在 ContextMenu Unloaded 事件中,刷新视图。比如:

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

这基本上就是博客中的补丁所做的。只是在完全不同的背景下。所以我的问题是无法使用像 ScrollTo() 这样的函数。在实际页面后面的代码中执行此操作似乎可以解决 ContextMenu 绑定问题。

【讨论】:

    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    相关资源
    最近更新 更多