How do you bind a CollectionContainer to a collection in a view model?

The CompositeCollection has no DataContext, the bindings in the CollectionContainers will not work if they bind directly to a property (which implicitly uses the DataContext as source).

You need to explicitly specify a source, i would suggest you name the control with your DataContext and use x:Reference to get it (ElementName will not work) or you use a StaticResource, e.g.

<CollectionContainer Collection="{Binding DataContext.GreekGods, Source={x:Reference myStackPanel}}"/>
<CollectionContainer Collection="{Binding GreekGods, Source={StaticResource CompositeCollectionVM}}"/>

Note that when using x:Reference the compiler easily trips you up with cyclical dependency errors, to avoid those place your CompositeCollection in the resources of the control you reference, then insert it wherever it belongs using the StaticResource markup extension.

Leave a Comment