I’m tying to generically draw an ObservableCollection of System.Windows.Shapes.Shape on a canvas using an ItemsControl but I am not sure about how to write the XAML to do it properly.
I've seen many posts regarding this topic, but they all seem to be tied to specific shape types. For example, from this example I realize I could define an ItemsContainer and set its template to draw lines, circles, etc.
<ItemsControl ItemsSource="{Binding Lines}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Line X1="{Binding From.X}" Y1="{Binding From.Y}"
X2="{Binding To.X}" Y2="{Binding To.Y}"
Stroke="DarkGray" StrokeThickness="3"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But this requires me to expose my shapes as separately typed collections of lines, circles, etc. -- or at least write some sort of filtering properties that do that. I was hoping to just expose just one collection of shapes. (I will be adding new shape types later on and don't want to have to keep adding new collection properties)
- Is there a simple, "best" approach I can take to achieve this without exposing separate collection types?
- Should I be looking at ItemTemplateSelector? It seems to me that would have the same problem of needing specific shape types.
- Some other approach?