TreeDataTemplate does not generate children [11-preview5]
See original GitHub issueDescribe the bug
I am using TreeDataTemplate
to show a generated tree in a TreeView
. Starting in 11-preview5, only the root nodes are shown. Same behavior under nightly (11.0.999-cibuild0030252-beta).
Below is the most relevant XAML. I am using compiled bindings and there are no warnings in the VS Output window. The node Children
collections are populated in the debugger. Collections are created once in the constructor and are never replaced. IIRC, there were no TreeViewItem
node children in 11-preview5+ in DevTools, only for the root nodes.
<TreeView Items="{Binding Items}">
<TreeView.DataTemplates>
<TreeDataTemplate DataType="vm:BranchNodeViewModel" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Children.Count, StringFormat=[BranchNode - Children: {0}]}" />
</TreeDataTemplate>
<TreeDataTemplate DataType="vm:LeafNodeViewModel" ItemsSource="{Binding Children}">
<TextBlock Text="[LeafNode]" />
</TreeDataTemplate>
</TreeView.DataTemplates>
</TreeView>
To Reproduce
- Clone this repro
- Run the app to see the top
Grid
TreeView
works under 11-preview4 - Update to 11-preview5 (or nightly), fixup
IControl
->Control
inViewLocator
, run the app, and see it no longer generates children.
Expected behavior I expect that the nodes with children are expandable.
Screenshots
The repro has a TreeDataTemplate
generated hierarchy on the top pane and the bottom pane has one that defines a hierarchy manually via TreeViewItem
.
11-preview4 correctly has expanders and children for the templated-generated pane:
11-preview5 (and nightly) do not:
Desktop (please complete the following information):
- OS: Windows 10
- Version: 11-preview4 (working), 11-preview5 (not working), 11.0.999-cibuild0030252-beta (not working)
Issue Analytics
- State:
- Created 7 months ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
Ok, I see what’s happening. If one removes the view locator in
App.axaml
in the repro then it starts working.The cause is the following:
ItemsControl
tries to find the data template, it first tries to find it relative to theTreeViewItem
ViewLocator
inApp.axaml
thenhic.FindDataTemplate(item)
returns null, so it then tries to find it relative to theTreeView
(the code after the??
)ViewLocator
inApp.axaml
though, this data template is found and used, meaning that the code after??
is never executedI think we need to delay the search for the item template until the item is attached to the tree.
I found a workaround: https://github.com/stevemonaco/AvaloniaTreeDataTemplateRepro/pull/1
The reason seems to be that only
ItemTemplate
is respected since preview5. If this is a bug or intended, I cannot say.