CollectionViewSource.GetDefaultView is not in Silverlight 3! What job?

The CollectionViewSource.GetDefaultView() method is missing in Silverlight 3. In WPF, I have this extension method:

 public static void SetActiveViewModel<ViewModelType>(this ViewModelBase viewModel, ViewModelType collectionItem, ObservableCollection<ViewModelType> collection) where ViewModelType : ViewModelBase { Debug.Assert(collection.Contains(collectionItem)); ICollectionView collectionView = CollectionViewSource.GetDefaultView(collection); if(collectionView != null) collectionView.MoveCurrentTo(collectionItem); } 

How can this be written in Silverlight 3?

+4
source share
2 answers

Silverlight does not contain a default presentation concept. When you request a control in Silverlight to bind to a collection, it really binds to the collection, it does not bind to the default view.

As a result, I don’t think there can be a direct and full port of your extension method. Some reorganization of your MVVM implementation will be necessary. I did not come up with the concept of a collection of instances of the view model, so I'm not quite sure what would be appropriate in your case.

A few approaches that I have seen with CollectionViewSource are either to have a CollectionViewSource defined in Xaml and bind its Source to something in the ViewModel. Alternatively, the ViewModel provides the CollectionViewSource property and binds the View xaml to its View proeprty.

+2
source

One thing you could do is manually create a CollectionViewSource, set its Source property to the collection, and then get the CollectionView using the View property of the CollectionViewSource.

Something like this might work:

 public static void SetActiveViewModel<ViewModelType>(this ViewModelBase viewModel, ViewModelType collectionItem, ObservableCollection<ViewModelType> collection) where ViewModelType : ViewModelBase { Debug.Assert(collection.Contains(collectionItem)); CollectionViewSource collectionViewSource = new CollectionViewSource(); collectionViewSource.Source = collection; ICollectionView collectionView = collectionViewSource.View; if(collectionView != null) collectionView.MoveCurrentTo(collectionItem); } 
+1
source

Source: https://habr.com/ru/post/1305322/


All Articles