【问题标题】:Why does the WPF Databinding not update after the collection is Filtered?为什么在过滤集合后 WPF 数据绑定不更新?
【发布时间】: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


【解决方案1】:

当从集合中添加或删除项目时,可观察集合发送属性更改通知事件,当项目的内容发生更改时,它不会发送更新。尝试自己发送通知,看看您的 WPF 视图是否更新。

【讨论】:

  • 谢谢,但我已经实现了 INotifyPropertyChanged,这没有帮助。
【解决方案2】:

使用可以帮助您的 DynamicResource 而不是使用 StaticResource。如下所示

ItemsSource="{Binding Source={DynamicResource ListBoxViewSource}}"

【讨论】:

    猜你喜欢
    • 2011-02-25
    • 2023-03-23
    • 1970-01-01
    • 2020-02-21
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多