Is it possible to explicitly use CollectionViewSource inside a data template? Usually we added CollectionViewSource to the resources along with the template, but our model does not allow this, because the "source" of the viewview collection is a DataContext property at this level in the tree, that is, there must be an instance at this level. Bringing it to the root of the resources would mean only one instance. We also cannot simply use grouping externally, because these elements do not exist until you are so far from the hierarchy, and not all brothers and sisters even have this property. Therefore, it is logical that we create an instance of CollectionViewSource in a DataTemplate (in this case, HierarchicalDataTemplate, but it does not matter.)
In particular, we are trying to allow specific sorting at this node level. Our only choice is to sort in the ViewModel itself, but it becomes a pain since we use ObservableCollections, which themselves do not support sorting. In fact, every article that we saw on this topic claims that you should use CollectionViewSource for this very reason, so this question.
For example, this works ...
<HierarchicalDataTemplate x:Key="CategoryTemplate"
ItemTemplate="{StaticResource TreeViewSymbolTemplate}"
ItemsSource="{Binding Symbols}">
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
</HierarchicalDataTemplate>
But it is not...
<HierarchicalDataTemplate x:Key="CategoryTemplate"
ItemTemplate="{StaticResource TreeViewSymbolTemplate}">
<HierarchicalDataTemplate.ItemsSource>
<Binding>
<Binding.Source>
<CollectionViewSource Source="{Binding Symbols}" />
</Binding.Source>
</Binding>
</HierarchicalDataTemplate.ItemsSource>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
</HierarchicalDataTemplate>
I seem to like it, but it is not. Again, we cannot bring the CollectionViewSource to the same level as the data template, since there must be one instance for each template, since each of them has its own set of elements (although they will all share the sorting criteria.)
M