You can define DataTemplates that apply to any instance of a specific type by specifying the DataType
without an x:Key
. Using this method you don't assign anything to ItemTemplate
- the templates are applied automatically.
<ListBox ItemsSource="{Binding Path=MixedList}">
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:BananaViewModel}">
<TextBlock Text="{Binding Name}" Foreground="Yellow"/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:AppleViewModel}">
<TextBlock Text="{Binding Name}" Foreground="Red"/>
</DataTemplate>
</ListBox.Resources>
</ListBox>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…