在 ObservableCollection 上应用 where 不会反映在数据网格上
我尝试“过滤” ObservableCollection 并更新绑定的 DataGrid。
ObservableCollection<Record> recordObservableCollection;
recordObservableCollection = new ObservableCollection<Record>(GetData()); //GetData() returns IEnumerable<Record>
dataGrid1.ItemsSource = recordObservableCollection;
然后我尝试过滤这个集合:
recordObservableCollection = new ObservableCollection<Record>(recordObservableCollection.Where(filter));//filter is Func<Data.Record, bool>
recordObservableCollection
更新得很好。
但 DataGrid 未更新。
I try to "filter" an ObservableCollection and update the bound DataGrid.
ObservableCollection<Record> recordObservableCollection;
recordObservableCollection = new ObservableCollection<Record>(GetData()); //GetData() returns IEnumerable<Record>
dataGrid1.ItemsSource = recordObservableCollection;
Then I try to filter this collection:
recordObservableCollection = new ObservableCollection<Record>(recordObservableCollection.Where(filter));//filter is Func<Data.Record, bool>
recordObservableCollection
is updated fine.
But the DataGrid is not updated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的名为
recordObservableCollection
的字段或变量最初有一个值,过滤后有一个不同的值。因为您使用了new ObservableCollection(...)
两次,所以您创建了两个单独的可观察集合实例。问题在于
DataGrid
仍然引用第一个实例。即使您更改了recordObservableCollection
,也只会影响其值。DataGrid.ItemsSource
的值仍然是过滤之前的值。要解决此问题,您需要将新集合的值重新分配给
ItemSource
属性。只需重复您第一次执行的操作,但在过滤之后:现在
ItemSource
将设置为recordObservableCollection
的新值。Your field or variable called
recordObservableCollection
has one value initially and a different value after filtering. Because you usednew ObservableCollection<Record>(...)
twice you created two separate observable collection instances.The problem is that the
DataGrid
is still referring to the first instance. Even though you have changedrecordObservableCollection
, that only affects its value. The value ofDataGrid.ItemsSource
is still what it was before the filtering.To fix this problem, you need to re-assign the new collection's value to the
ItemSource
property. Simply repeat what you did the first time, but after the filtering:and now
ItemSource
will be set to the new value ofrecordObservableCollection
.ObservableCollection 将得到更新,因为 ObservableCollection (System.Collections.ObjectModel) 每次集合更改时都会抛出一个事件,但您必须再次将过滤器集合设置为 itemsource,否则它不会更新 UI...
执行此操作的最佳方法是使用您将在控件中将其绑定为项目源的公共属性,并且在该属性中,您将在 setter 中定义 NotifyPropertyChanged 。每次您使用此属性更改集合时,控件也会更新...
假设您的数据网格位于 test.xaml
-->首先,INotifyPropertyChanged 的所有工作在项目中添加一个抽象类,从 INotifyPropertyChanged 接口继承它并定义 OnPropertyChanged 方法
-->在您的项目中添加一个名为 testViewModel 的类后,它将继承您的 ViewModelBase 类..
-->现在在 testViewModel 中,您将为网格绑定创建一个属性,如下所示
}
现在,如果您使用任何其他属性上的属性更新您的集合,方法或命令 UI 将更新,因为您已定义 OnPropertyChanged 的 setter 中...
现在返回对于这里的 test.xaml,您必须做两件事
在代码中或在 xaml 中设置 test.xaml 的 dataContext
(在InitializeComponent()之后的代码中,创建一个viewmodel类的实例并将其分配为DataContext,如下所示
<前><代码>公共测试()
{
初始化组件();
testViewModel vm = new testViewModel();
this.DataContext = vm;
}
将您在testViewModel中定义的属性绑定到网格
ObservableCollection will get update because ObservableCollection (System.Collections.ObjectModel) throws an event every time the collection get changed but you have to set the filter collection as itemsource again other wise it wont update the UI...
The best way to do this use a public property that you'll bind in control as item source and in that property you will define NotifyPropertyChanged in setter . Every time you'll change the collection using this property the control will also be updated ...
Let Suppose you have your data grid in test.xaml
--> First fo all work for INotifyPropertyChanged add an abstract class in your project inherit it from INotifyPropertyChanged interface and define OnPropertyChanged method
--> After add a class in your project named it testViewModel which will inherit your ViewModelBase Class..
--> Now in testViewModel you'll make a property for your grid binding like this
}
here now if u update your collection using property on any other property, method or command UI will be updated beacsue in setter you have defined OnPropertyChanged...
Now comes back to test.xaml here you have to do two things
Set dataContext of test.xaml either in code behing or in in xaml
(In Code behind just after InitializeComponent() make an intance of viewmodel class and assign it as DataContext like this
Bind property you defined in testViewModel to grid
将
ObservableCollection
公开为公共属性,并且
仅当您从列表中添加/删除项目时,使用
ObservableCollection
才会影响绑定。通过使用ObservableCollection
,当集合发生变化(而不是集合内的项目发生变化)时,您无需重置与列表或DataGrid
的绑定。但当您的数据对象属性发生更改时,它们不会产生任何影响。为此,您需要为 DataObject 实现 INotifyPropertyChanged 接口。Expose the
ObservableCollection<Record>
as a public propertyalso
Using
ObservableCollection
only affect binding when you add/remove items from your list. By usingObservableCollection
you do not need to reset binding to the list orDataGrid
when your collection changed (not the item inside collection changed). But they do not have any effect when your data object properties changed. For that you need to implementINotifyPropertyChanged
interface for your DataObject.