Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
727 views
in Technique[技术] by (71.8m points)

xaml - Automatic template selection in WPF not working with interface

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Am I doing something wrong here, or does WPF just not support this type of automatic template selection based on interfaces?

You are not doing something wrong. This kind of data binding support for interfaces is simply not supported. Please refer to Beatriz Costa's (MSFT) answer in the following thread on the MSDN forums for more information about why.

Data templates and interfaces: https://social.msdn.microsoft.com/Forums/vstudio/en-US/1e774a24-0deb-4acd-a719-32abd847041d/data-templates-and-interfaces?forum=wpf

"The data binding team discussed adding support for interfaces a while ago but ended up not implementing it because we could not come up with a good design for it. The problem was that interfaces don't have a hierarchy like object types do. Consider the scenario where your data source implements both IMyInterface1 and IMyInterface2 and you have DataTemplates for both of those interfaces in the resources: which DataTemplate do you think we should pick up?

When doing implicit data templating for object types, we first try to find a DataTemplate for the exact type, then for its parent, grandparent and so on. There is very well defined order of types for us to apply. When we talked about adding support for interfaces, we considered using reflection to find out all interfaces and adding them to the end of the list of types. The problem we encountered was defining the order of the interfaces when the type implements multiple interfaces."

So you will either have to define a DataTemplate for both Tile and TileRun explicitly or use a DataTemplateSelector.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...