当过滤器的项目属性更改时,数据网格未更新

发布于 2024-09-06 22:03:35 字数 574 浏览 5 评论 0原文

我有一个带有数据网格和按钮的简单表单。数据网格中的项目绑定到 customersObservableCollectioncustomer 实现 INotifyPropertyChanged 接口。每个客户都有一个deleted 属性(bool 类型)。我为客户默认视图设置了一个过滤器,以根据 deleted 属性过滤掉已删除的客户。到目前为止它有效。

然后,我为按钮添加一个事件,将所选客户标记为已删除。问题是设置 selected_customer.deleted = true 后网格未更新。已删除的客户仍然可见。绑定到 deleted 属性的列正确更新。要从网格中删除客户,我必须手动调用客户默认视图的 Refresh() 方法。

为什么当我使用 ObservableCollection 并且客户实现 INotifyPropertyChanged 接口时,数据网格没有自动更新?如何让它自动更新?

I have a simple form with datagrid and a button. Items in the datagrid are bound to ObservableCollection of customers. The customer implements INotifyPropertyChanged interface. Each customer has a deleted property (type bool). I set a filter for the customers default view to filter out deleted customers based on the deleted property. So far it works.

Then I add an event for the button that marks selected customer as deleted. The problem is the grid is not updated after setting selected_customer.deleted = true. The deleted customer is still visible. The column bound to deleted property updates correctly. To remove the customer from the grid, I have to manually call Refresh() method of the customers default view.

Why is not the datagrid updated automaticaly when I use ObservableCollection and the customer implements INotifyPropertyChanged interface? How to make it update automatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三岁铭 2024-09-13 22:03:35

我假设您使用 CollectionViewSource 进行过滤。
下面的代码将订阅客户的Deleted属性的更改,并在Deleted更改时刷新collectioviewsource。 Customers 是 Customer 类的 ObservableCollection。客户有一个名为Deleted 的布尔属性并实现了INotifyPropertyChanged。应在填充客户之前调用 InitAutoRefresh()。

private void InitAutoRefresh(ObservableCollection<Customer> observableCollection, CollectionViewSource collectionViewSource)
{
    observableCollection.CollectionChanged += 
        (sender, e) =>
        {
            foreach(Customer newItem in e.NewItems)
            {
                newItem.PropertyChanged += 
                    (sender2, e2) =>
                    {
                        if (e2.PropertyName == "Deleted")
                        {
                            collectionViewSource.View.Refresh();
                        }
                    };
            }
        };
}

在填充可观察集合之前调用它。如果您在 XAML 中声明了 collectionViewSource,则可以使用 FindResource 来获取实例。

InitAutoRefresh(Customers, FindResource("cvsCustomers") as CollectionViewSource);

I assume you use a CollectionViewSource for filtering.
Below code will subscribe to changes to Deleted property of customer and refresh the collectioviewsource when Deleted changes. Customers is an ObservableCollection of class Customer. Customer has a bool property called Deleted and implements INotifyPropertyChanged. InitAutoRefresh() should be called before populating Customers.

private void InitAutoRefresh(ObservableCollection<Customer> observableCollection, CollectionViewSource collectionViewSource)
{
    observableCollection.CollectionChanged += 
        (sender, e) =>
        {
            foreach(Customer newItem in e.NewItems)
            {
                newItem.PropertyChanged += 
                    (sender2, e2) =>
                    {
                        if (e2.PropertyName == "Deleted")
                        {
                            collectionViewSource.View.Refresh();
                        }
                    };
            }
        };
}

Call it before you populate the observable collection. If you declared your collectionViewSource in XAML you can use FindResource to get the instance.

InitAutoRefresh(Customers, FindResource("cvsCustomers") as CollectionViewSource);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文