She*_*don 5 c# silverlight-toolkit silverlight-4.0 windows-phone-8
我在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>
Run Code Online (Sandbox Code Playgroud)
这是处理菜单项上的click事件的方法
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();
}
Run Code Online (Sandbox Code Playgroud)
以下方法填充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");
}
Run Code Online (Sandbox Code Playgroud)
因此,当SavedGames集合首次普及时,它可以完美地工作,但是当集合发生变化时(删除一些旧项目,添加新项目),我会发现一些奇怪的行为.当OnClick事件被触发时,我看到menuItem.DataContext不是我点击的菜单项,而是一些被删除的旧菜单项.
我不能对你的帖子发表评论所以我会在这里说:
这是一个已知的问题,我也有.我没有找到任何方法来完全解决这个问题,也没有看到任何最近的解决方案.您可以在此处查看我的帖子,以确保问题符合您的要求.
我到目前为止看到的唯一解决方案是在这个来自'11的msdn博客中描述的.它确定了Silverlight框架中的问题,并提供了我实现的解决方法.在项目中包含类文件并使用XAML标记,它将允许您的上下文菜单与父级的datacontext保持同步.我使用它遇到了一个小副作用,所以它只是一个乐队援助.
我还发现从另一个论坛告诉它这是一个已知问题没有解决方案,但可以在codeplex找到补丁.我对补丁的问题是我无法弄清楚如何实现它,而LLS(我正在使用ContextMenu)已经直接迁移到SDK中,所以我被困住了.
这就是我在这个问题上挖出的所有内容,希望它有所帮助.如果其他人已经添加了,请做.
更新: 使用上面提供的链接中的一些内容,我认为我有一个更好的解决方案.在ContextMenu Unloaded事件中,刷新视图.就像是:
private void add_but_up(object sender, RoutedEventArgs e)
{
ContextMenu conmen = (sender as ContextMenu);
conmen.ClearValue(FrameworkElement.DataContextProperty);
}
Run Code Online (Sandbox Code Playgroud)
这基本上就是博客中的补丁.只有在完全不同的背景下.所以我的问题是无法使用像ScrollTo()这样的函数.在实际页面后面的代码中执行此操作似乎修复了ContextMenu绑定问题.
| 归档时间: |
|
| 查看次数: |
965 次 |
| 最近记录: |