-
Notifications
You must be signed in to change notification settings - Fork 814
TreeView with DataTemplateSelector not reliably using selected DataTemplate #2121
Description
Describe the bug
I have a tree view. I want to fill it with 2 types of data - one that can have children and one that can not. The template is selected with an DataTemplateSelector which checks the node.Content type for the selection.
The data is given in 2 lists that get iterated one after another. The root nodes are given based upon the selection of a combobox - a new selection clears the treeview and repopulates it.
After an Item is added, the task is delayed for a few milliseconds to avoid lagging gui because the layouting can't keep up when adding a hundred elements to the treeview. When starting to add the second type, the ItemTemplateSelector selects the correct template (I check that with Debug logging), but the TreeView still uses the template of the first item type, causing a crash with anInvalidCastException when attempting to cast the node.Content.
This does not happen when I let the TreeView fully populate, but only when I cancel it by clearing the RootNodes and repopulate them with new data.
Steps to reproduce the bug
- clone https://github.com/Shad0wlife/TreeViewCrashSampleApp
- Build it
- Run it
- Select an element in the Combobox
- While you see the elements loading, select another element in the combobox
- this clears the list and should reload it with the new data
- you should crash with an InvalidCastException now sometimes
- this does not happen reliably sadly
- if the list loaded without error once, you will not manage to have it error again in that session, you'll have to restart the program
Expected behavior
The selected template of the DataTemplateSelector is respected for every node added.
Screenshots
The Error with log showing that the correct template was given by the selector:

The visual representation with non-crashing templates:
It can be seen that the nodes get the corresponding HasUnrealizedChildren value, but the Template used is the wrong one.

Version Info
Package version: 2.4.0-prerelease.200203002
OS Version: 1809
Project Target: 1903 (required by package)
Project Minimum: 1809
NuGet package version:
Microsoft.UI.Xaml
Package version: 2.4.0-prerelease.200203002
| Windows 10 version | Saw the problem? |
|---|---|
| Insider Build (xxxxx) | |
| November 2019 Update (18363) | |
| May 2019 Update (18362) | |
| October 2018 Update (17763) | Yes |
| April 2018 Update (17134) | |
| Fall Creators Update (16299) | |
| Creators Update (15063) |
| Device form factor | Saw the problem? |
|---|---|
| Desktop | Yes |
| Mobile | |
| Xbox | |
| Surface Hub | |
| IoT |
Additional context
This might also be an issue with how I implemented things, so If you see the issue somewhere in my code please tell me!
If you want to see the effect yourself without crashing, you can enable the debugging templates in MainPage.xaml instead of the full Templates. These templates are visually distinct but won't crash due to casts.
EDIT: I kept in the Attributes for my Database code that the full application uses in case they impact the result due to possible extra load or so. That said: the linked repo is obviously a sample app created from a larger project cut down to a standalone program to show the issue. I just wanted to keep all code affecting any of the process from data to layout as intact as possible.