question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TreeDataTemplate does not generate children [11-preview5]

See original GitHub issue

Describe 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

  1. Clone this repro
  2. Run the app to see the top Grid TreeView works under 11-preview4
  3. Update to 11-preview5 (or nightly), fixup IControl -> Control in ViewLocator, 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: image

11-preview5 (and nightly) do not: image

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:closed
  • Created 7 months ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
grokyscommented, Feb 27, 2023

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:

  • PanelContainerGenerator prepares the item before adding it to the logical tree
  • When ItemsControl tries to find the data template, it first tries to find it relative to the TreeViewItem
  • If there is no ViewLocator in App.axaml then hic.FindDataTemplate(item) returns null, so it then tries to find it relative to the TreeView (the code after the ??)
  • If there is a ViewLocator in App.axaml though, this data template is found and used, meaning that the code after ?? is never executed

I think we need to delay the search for the item template until the item is attached to the tree.

1reaction
timuniecommented, Feb 20, 2023

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found