The CollectionViewSource.GetDefaultView() method is not 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?
Silverlight does not contain the concept of a Default view. When you ask a control in Silverlight to bind to a collection it really does bind to the collection, it does not bind to a default view.
As result I don’t think there can be a direct and complete port of your extension method. Some re-engineering of your MVVM implementation will be needed. I’ve not come across the concept of a collection of view model instances before so I’m not exactly sure what would be appropriate in your case.
A couple of approaches I’ve seen with
CollectionViewSourceis to either have theCollectionViewSourcedefined in the Xaml and bind itsSourceto something in the ViewModel. Alternatively have a ViewModel expose aCollectionViewSourceproperty and have the View xaml bind to itsViewproeprty.