WPF:要使用的集合类

时间:2021-11-05 19:03:03

After spending a whole day trying different suggestions, I'm back at square 1. I'm trying to bind my view, a XAML Window, to one of my ViewModel properties, say, SalesOrders. The ViewModel in turn talks to the Model (an EF Model on top of a database). The question I'm facing is the collection type that I should use to expose my SalesOrders property.

在花了一整天尝试不同的建议之后,我回到了正方形1.我正在尝试将我的视图(一个XAML窗口)绑定到我的一个ViewModel属性,比如SalesOrders。 ViewModel又与模型(数据库顶部的EF模型)进行对话。我面临的问题是我应该使用的集合类型来公开我的SalesOrders属性。

I have tried the following types, none of which does all of what I need.

我尝试了以下类型,其中没有一个能满足我的需要。

  1. List<T>
  2. ObservableCollection<T>
  3. BindingList<T>
  4. CollectionViewSource on top of the above
  5. CollectionViewSource在上面的上面

Here's what I need my collection to do:

这是我需要我的收藏品做的事情:

  1. The view has Previous/Next buttons, so the collection should provide some sort of currency manager.
  2. 该视图具有上一个/下一个按钮,因此该集合应提供某种货币管理器。

  3. There's a Save button in the view, which I need to get enabled/disabled immediately based on whether the SalesOrder collection has any changes. Since SalesOrder is already an EF type, all of its fields implement INotifyPropertyChanged.
  4. 视图中有一个“保存”按钮,我需要根据SalesOrder集合是否有任何更改立即启用/禁用。由于SalesOrder已经是EF类型,因此其所有字段都实现了INotifyPropertyChanged。

CollectionViewSource provides me with navigation methods (previous/next) but doesn't listen to PropertyChanged events, so modifying data in the view doesn't turn the Save button on. BindingList can listen to PropertyChanged events, but doesn't provide navigation methods. ObservableCollection lacks both functionalities.

CollectionViewSource为我提供了导航方法(上一个/下一个)但不监听PropertyChanged事件,因此修改视图中的数据不会打开“保存”按钮。 BindingList可以侦听PropertyChanged事件,但不提供导航方法。 ObservableCollection缺乏两种功能。

TIA.

2 个解决方案

#1


0  

Why don't you use ObservableCollection<T> then subscribe to the CollectionChanged event to enable or disable your save button as outlined in the answer of the thread MVVM ObservableCollection Bind TwoWay.

为什么不使用ObservableCollection 然后订阅CollectionChanged事件以启用或禁用保存按钮,如线程MVVM ObservableCollection Bind TwoWay的答案中所述。

#2


0  

According to MSDN about CollectionView here:

根据MSDN关于CollectionView的信息:

In WPF applications, all collections have an associated default collection view. Rather than working with the collection directly, the binding engine always accesses the collection through the associated view. To get the default view, use the CollectionViewSource.GetDefaultView method. An internal class based on CollectionView is the default view for collections that implement only IEnumerable. ListCollectionView is the default view for collections that implement IList. BindingListCollectionView is the default view for collections that implement IBindingListView or IBindingList.

在WPF应用程序中,所有集合都具有关联的默认集合视图。绑定引擎不是直接使用集合,而是始终通过关联视图访问集合。要获取默认视图,请使用CollectionViewSource.GetDefaultView方法。基于CollectionView的内部类是仅实现IEnumerable的集合的默认视图。 ListCollectionView是实现IList的集合的默认视图。 BindingListCollectionView是实现IBindingListView或IBindingList的集合的默认视图。

Which means you can use BindingList for SalesOrders and bind it in the View, then to manage the navigation you can access its automatically created CollectionView from the ViewModel with:

这意味着您可以将BindingList用于SalesOrders并将其绑定到View中,然后管理导航,您可以从ViewModel访问其自动创建的CollectionView:

myCollectionView = (BindingListCollectionView)CollectionViewSource.GetDefaultView(this.SalesOrders);

#1


0  

Why don't you use ObservableCollection<T> then subscribe to the CollectionChanged event to enable or disable your save button as outlined in the answer of the thread MVVM ObservableCollection Bind TwoWay.

为什么不使用ObservableCollection 然后订阅CollectionChanged事件以启用或禁用保存按钮,如线程MVVM ObservableCollection Bind TwoWay的答案中所述。

#2


0  

According to MSDN about CollectionView here:

根据MSDN关于CollectionView的信息:

In WPF applications, all collections have an associated default collection view. Rather than working with the collection directly, the binding engine always accesses the collection through the associated view. To get the default view, use the CollectionViewSource.GetDefaultView method. An internal class based on CollectionView is the default view for collections that implement only IEnumerable. ListCollectionView is the default view for collections that implement IList. BindingListCollectionView is the default view for collections that implement IBindingListView or IBindingList.

在WPF应用程序中,所有集合都具有关联的默认集合视图。绑定引擎不是直接使用集合,而是始终通过关联视图访问集合。要获取默认视图,请使用CollectionViewSource.GetDefaultView方法。基于CollectionView的内部类是仅实现IEnumerable的集合的默认视图。 ListCollectionView是实现IList的集合的默认视图。 BindingListCollectionView是实现IBindingListView或IBindingList的集合的默认视图。

Which means you can use BindingList for SalesOrders and bind it in the View, then to manage the navigation you can access its automatically created CollectionView from the ViewModel with:

这意味着您可以将BindingList用于SalesOrders并将其绑定到View中,然后管理导航,您可以从ViewModel访问其自动创建的CollectionView:

myCollectionView = (BindingListCollectionView)CollectionViewSource.GetDefaultView(this.SalesOrders);