为什么在筛选集合后 WPF 数据绑定不更新?
为什么更新集合时我的 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 后,不会调用该转换器。
Why does my WPF ContextMenu databinding not update as expected when the collection is updated, however the ItemTemplate is displaying the context menu text correctly?
Within the code below this works when I don't try update the ObservableCollection. When the underlying ObservableCollection is updated the DataTemplate updates without problems and displays the new MenuItem text as expected. However the MenuItem.Tag is returning Nothing after the refresh occurs?
The data bound MenuItem.Tag works when the collection is first loaded however not after I update. Any ideas on how I can locate this error? Partial XAML code is shown below:
<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>
Update #1: The issue seems to be related to the CollectionViewSource and the Filter not updating the MenuItem.Tag binding after filtering has occurred and removed the item. I have added CollectionViewSource.View.Refresh() where the collection would be changing however this still does not fix the issue.
Update #2 I have already implemented INotifyPropertyChanged which does not help with the Filtering issue. If I remove the Filter on the CollectionViewSource then the issue doesn't occur. I also added a converter to the MenuItem.Tag binding and this doesn't get called after the filtering is applied and the MenuItem.Tag is then set to Nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当从集合中添加或删除项目时,可观察集合会发送属性更改通知事件,而当项目的内容发生更改时,它不会发送更新。尝试自己发送通知并查看您的 WPF 视图是否更新。
The observable collection sends property change notification events when items are added or removed from the collection, it doesn't send updates when the content of the items is changed. Try sending the notifications yourself and see if your WPF view updates.
使用 DynamicResource 代替 StaticResource,它可以帮助您。就像下面这样
Instead of using StaticResource use DynamicResource which can helps you. Like as follows