【发布时间】:2009-08-28 09:05:43
【问题描述】:
为什么更新集合时我的 WPF ContextMenu 数据绑定没有按预期更新,但 ItemTemplate 正确显示上下文菜单文本?
当我不尝试更新 ObservableCollection 时,这在下面的代码中有效。当基础 ObservableCollection 更新时,DataTemplate 更新没有问题,并按预期显示新的 MenuItem 文本。但是刷新后 MenuItem.Tag 没有返回任何内容?
数据绑定 MenuItem.Tag 在首次加载集合时有效,但在我更新后无效。关于如何找到此错误的任何想法?部分 XAML 代码如下所示:
<ListBox ItemsSource="{Binding Source={StaticResource ListBoxViewSource}}">
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel>
<WrapPanel.ContextMenu>
<ContextMenu>
<Separator/>
<MenuItem ItemsSource="{Binding Source={StaticResource ContextViewSource}}" ItemTemplate="{StaticResource DataTemplate}">
<MenuItem.Tag>
<Binding Path="ID" Source="{StaticResource ContextViewSource}"/>
</MenuItem.Tag>
更新 #1: 该问题似乎与 CollectionViewSource 和 Filter 在过滤发生并删除项目后未更新 MenuItem.Tag 绑定有关。我添加了 CollectionViewSource.View.Refresh() ,其中集合将发生变化,但这仍然不能解决问题。
更新 #2 我已经实现了 INotifyPropertyChanged,它对过滤问题没有帮助。如果我删除 CollectionViewSource 上的过滤器,则不会发生此问题。我还在 MenuItem.Tag 绑定中添加了一个转换器,在应用过滤后不会调用它,然后将 MenuItem.Tag 设置为 Nothing。
【问题讨论】:
-
这个你搞定了吗?
标签: .net wpf data-binding filter collectionviewsource