I have a simple application with a TreeView which I would like to display a Hierarchy of SHAPES. I am using MVVM, so I have a ViewModel, which has a property of ObservableCollection Shapes.
ShapeVm is the base class, and there are several Shape subtypes (SquareVm, CircleVm, ArcVm etc). Pretty basic stuff. The ShapeVm baseclass has an overidable property called ShapeType (an enum: enum { Square, Circle, Arc }, and each ShapeVm subclass provides what type of Shape it is.
Now, you can see where this is going: In the TreeView, all I need is a hierachical display of these shapes, with 'ShapeType' nodes, and each node containing the shapes of that type.
So, I have tried various things, in XAML, like the CollectionViewSource, to provide a means of grouping by ShapeType, which works, but it only shows the items, and groups, that EXIST in the list.
What I need, in the TreeView, is to have those Group nodes displayed, whether they have items or not.
so, for instance (the Windows' DataContext is bound to the ShapesViewModel, which has the 'Shapes' property)
<TreeView>
<!-- Itemtemplate omitted here -->
<TreeViewItem Header="Rectangles" IsExpanded="true" ItemsSource="{Binding Shapes}">
</TreeViewItem>
<TreeViewItem Header="Squares" IsExpanded="true" ItemsSource="{Binding Shapes}">
</TreeViewItem>
<TreeViewItem Header="Circles" IsExpanded="true" ItemsSource="{Binding Shapes}">
</TreeViewItem>
</TreeView>
Now, I have fiddled with my ViewModel, where I expose a property, a collection, of each Type of shape, that gets its data from the main shapes collection. I know that the ViewModels' job is supposed to be there to 'serve' the View with whatever the View needs to do it's job. But I don't want to have to have a whole bunch of public Collection types, each of which need to somehow 'sync' with the main Shapes collection every time that changes etc. It gets complicated.
I just want to know if there is a way to, within the XAML, to say: "Hey, 'Rectangle' node, you must display only 'Rectangles', from the Shapes collection, ok.".
Help appreciated.