当过滤器的项目属性更改时,数据网格未更新
我有一个带有数据网格和按钮的简单表单。数据网格中的项目绑定到 customers
的 ObservableCollection
。 customer
实现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您使用 CollectionViewSource 进行过滤。
下面的代码将订阅客户的Deleted属性的更改,并在Deleted更改时刷新collectioviewsource。 Customers 是 Customer 类的 ObservableCollection。客户有一个名为Deleted 的布尔属性并实现了INotifyPropertyChanged。应在填充客户之前调用 InitAutoRefresh()。
在填充可观察集合之前调用它。如果您在 XAML 中声明了 collectionViewSource,则可以使用 FindResource 来获取实例。
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.
Call it before you populate the observable collection. If you declared your collectionViewSource in XAML you can use FindResource to get the instance.