Question

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.

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

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. 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.

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.

TIA.

Was it helpful?

Solution

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.

OTHER TIPS

According to MSDN about CollectionView here:

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.

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:

myCollectionView = (BindingListCollectionView)CollectionViewSource.GetDefaultView(this.SalesOrders);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top