Binding a CollectionViewSource within a DataTemplate Binding a CollectionViewSource within a DataTemplate wpf wpf

Binding a CollectionViewSource within a DataTemplate


As I understand it, the DataTemplate acts as instructions on what to insert into the visual tree but does not become a part of the visual tree itself. I only came to this hypothesis after running into the same problem you've described above. I fixed the issue by attaching the CollectionViewSource to the resources of an element that would be part of the visual tree, in my case a grid. Here is the sample that did work:

<DataTemplate DataType="{x:Type TypedLists:AssetModelListViewModel}">    <Grid>        <Grid.Resources>            <CollectionViewSource x:Key="items"                                  Source="{Binding}">                <CollectionViewSource.SortDescriptions>                    <scm:SortDescription PropertyName="AssetType.AssetCategory.Name" />                    <scm:SortDescription PropertyName="AssetType.Name" />                    <scm:SortDescription PropertyName="Manufacturer.Name" />                </CollectionViewSource.SortDescriptions>            </CollectionViewSource>        </Grid.Resources>        <ListView ItemsSource="{Binding Source={StaticResource items}}">        </ListView>    </Grid></DataTemplate>


I worked around this issue by moving the data template into a user control.


I think you need to bind to the view of the CollectionViewSource:

<ListBox ItemsSource="{Binding Path=View, Source={StaticResource CVS}}">