Automatic template selection in WPF not working with interface

后端 未结 1 1193
野的像风
野的像风 2021-01-25 09:19

I have a TreeView bound to a list of Tileset. Tileset contains TileGroup, TileGroup contains both Tile

相关标签:
1条回答
  • 2021-01-25 09:48

    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.

    0 讨论(0)
提交回复
热议问题