I have a TreeView
bound to a list of Tileset
. Tileset
contains TileGroup
, TileGroup
contains both Tile
and TileRun
instances. Both Tile
and TileRun
implement ITile
, but eventually there will be many more types implementing ITile
I have the following XAML:
<TreeView
Grid.Row="0"
Grid.Column="0"
BorderThickness="0"
ItemsSource="{Binding Path=Tilesets}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Tileset}" ItemsSource="{Binding Path=TileGroups}">
<TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:TileGroup}" ItemsSource="{Binding Path=Tiles}">
<TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type tiles:ITile}">
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</TreeView.Resources>
</TreeView>
Tileset
and TileGroup
choose the correct DataTemplate
but ITile
does not, no template is selected, the tree just displays the data type.
However, if I add a DataTemplate
for both Tile
and TileRun
explicitly, everything works great.I don't want to do that though, as there will eventually be many more classes implementing ITile
.
I am aware that I could handle this using a DataTemplateSelector
, but I'd like a pure XAML solution if possible.
Am I doing something wrong here, or does WPF just not support this type of automatic template selection based on interfaces?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…