I have a WPF project that draws several things in a panel. For the next release I need to add another type of thing to draw in addition to the existing things. Currently I hav
Thank your for the improved code example. From that, it appears to me that you are going about your goal the wrong way entirely.
That is, you are trying to have a single ItemsControl
object render all of your lines. But that's not how your data model is organized. Your data model has two completely different kinds of objects: LineArt
objects, and the ObstacleArt
object that contains LineArt
objects.
Given that, it seems to me that a more appropriate approach would be to simply compose the MainWindowResource.Lines
and MainWindowResource.Obstacles
collections, and then use data templates to appropriately display these collections together.
Here is a new version of your XAML that shows what I mean:
The key here is the second DataTemplate
, with a target type of ObstacleArt
. This allows the main ItemsControl
to display the individual ObstacleArt
elements in the composite collection. Via the second data template, it does so by nesting an entirely new ItemsControl
within for each ObstacleArt
object, where that ItemsControl
handles all of the rendering for the ObstacleArt
object. Note that since the actual items in that nested ItemsControl
object are themselves LineArt
items, this winds up referring back to the DataTemplate
for the LineArt
type.
The above works without any changes at all to your code-behind. That said, it is my opinion that you would be better off making your classes inherit DependencyObject
, and then make the bindable properties dependency properties. WPF does support INotifyPropertyChanged
of course, but you have a lot of explicit property notification code in there that would just go away if you were using the dependency-property paradigm.