CollectionViewSource class is supposed to provide a so-called "view" over data. This includes data filtering, sorting and grouping.
Now, one may think, that this is a responsibility of a View in MVVM architecture, but very often it is user, who decides, what kind of grouping, sorting or filtering he wants. These are business decisions, which belongs to viewmodel. Moreover, filtering is performed by taking an element and deciding, whether it matches filter or not, and that is definitely viewmodel's responsibility (possibly then passed to a service).
It seems then, that CollectionViewSource is a good candidate to be directly exposed from the viewmodel, because the latter then have full control over its properties (eg. GroupDescriptions). But even though this class resides in Windows.Data namespace, it is still in PresentationFramework assembly and I feel really uncomfortable to reference it from my BusinessLogic assembly, because I'm hardly tying it to a specific (nomen omen) presentation framework.
From what I saw, the most common place for CollectionViewSource are control's (window's) resources from which (surprisingly) I can bind to the viewmodel. But then manipulating filtering, sorting and grouping is a mess, because you have to push information manually between view and viewmodel. Just for one example:
<CollectionViewSource x:Key="SuggestionItems" Source="{Binding Suggestions}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="{Binding SuggestionGroupByProperty}"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
This will not work, because PropertyGroupDescription.PropertyName is not a DependencyProperty. So I'd probably have to design my own Xaml extension to be able to bind such information from viewmodel to a view.
I failed to find any tutorial or documentation on how is ColletionViewSource supposed to fit into MVVM/WPF framework. I actually got an impression, that this is not really too much thought-through, because, for instance, placing the CollectionViewSource anywhere outside resources requires some really nasty shenanigans to simply push collection inside.
For example (mind the SelectedItem):
<DataGrid x:Name="grid"
AutoGenerateColumns="False"
ItemsSource="{Binding}"
SelectedItem="{Binding Path=DataContext.SelectedItem, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Mode=TwoWay}">
<DataGrid.DataContext>
<CollectionViewSource x:Name="myViewSource" Source="{Binding MyCollection}" Filter="CollectionViewSource_OnFilter">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Container" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</DataGrid.DataContext>
<!-- (...) -->
</DataGrid>
So how the CollectionViewSource should canonically fit into the MVVM pattern, so that viewmodel has enough control over it? And if it shouldn't, then how this scenario (collection grouping, sorting, filtering) should actually look in MVVM?
I use CollectionViewSource like this