It appears that you have the wrong idea about the ItemContainerStyle
... the clue is in the name... it is the Style
for the container, not for the content. By that I mean that it Style
s the ListBoxItem
and not the data contained within the item... literally, the container of the item.
What (it seems) you are really looking for is the DataTemplate
that you can specify for the ItemTemplate
property. The DataTemplate
has a DataType
property specifically so that you can take advantage of the properties of the relevant class. You could use a DataTemplateSelector
to switch between DataTemplate
s on a per item basis, but there is another way.
What you can do is to specify a DataTemplate
for each data type you have, but don't give them an x:Key
value. This way, WPF will automatically apply them to any relevant data type object that it finds that doesn't have one explicitly set.
<DataTemplate DataType="{x:Type YourXmlNamespacePrefix:SomeDataType}">
...
</DataTemplate>
<DataTemplate DataType="{x:Type YourXmlNamespacePrefix:SomeOtherDataType}">
...
</DataTemplate>
You can either leave the ItemContainerStyle
empty, or define what the container will look like for every item, regardless of the type.