-
Notifications
You must be signed in to change notification settings - Fork 700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ItemTemplateSelector is broken in all Collection Views (ListView, GridView, TreeView...) that has their default containers specified in the DataTemplate #9189
Comments
An alternative work around is to comment out the following override in the
The remarks section of SelectTemplateCore suggests that only the second override should be provided when using virtualising panels |
That's very good information. I haven't noticed that part yet. But still, you get the result of a broken view, as it is the same as putting your Template into an internally created Container. Which leads to your gridview and listview to have doubled layer (if you already put a Container in the DataTemplate), and treeview still have no way to set a ItemsSource. Which means we will still have to do similar things to set the ItemsSource value for the hidden real TreeViewItem. |
Hello. Thanks for your report. This turns out to be by design. In a nutshell, taking a ListView as an example, when the application uses a ListView.ItemTemplateSelector instead of a ListView.ItemTemplate, and that selector returns DataTemplate instances with a ListViewItem as its root element, that application must listen and handle the ListView.ChoosingItemContainer and ListView.ContainerContentChanging events. The ContainerContentChanging handler is in charge of building lists of recyclable containers (for each kind of item template returned by the ItemTemplateSelector). The ChoosingItemContainer is in charge of returning an appropriate container based on the provided item, if and only if the suggested container is inapplicable to the item. The returned container may be picked from those recyclable container lists or not. Adding such handlers to your repro application fixes the crashes. Which changes are required in the repro app, just for the ListView case?
We have a couple of potential ideas for improving the developer experience though:
Thank you. |
Describe the bug
ItemTemplateSelector is in a broken state for collection view controls (ListView, GridView, TreeView, etc) in WinUI3.
When the user scrolls through the views and triggers recycling of containers, the control will constantly choose an incompatible container for the new items that are brought into view. Which will often lead to broken views, binding failing and/or crashing of applications.
It is surprising that Microsoft have this glancing bug to live for so long since WinUI's release.
Workarounds and analysis
There is a Workarounds that can help to you avoid such a crashing: Don't use the default containers in the DataTemplate. 'Default Containers' here means ListViewItem for ListView, GridViewItem for GridViewView.
For Example, don't do
Instead, do
My analysis of how this workarounds works: If you directly provide a container corresponding to the view as the root frame of the DataTemplate, the ListViewBase will directly use it as the container of the new items. But if there is no valid container in the DataTemplate, the ListViewBase will create a new container and put your templated stuff into its Content field. In this case, since the Containers will always be the same, the view controls just replaced its contents, so that the container will always be compatible with each other.
Unfortunately, this workarounds cannot work for TreeViews. Because without a TreeViewItem in the DataTemplate, you basically have no way to specify ItemsSource for your nodes. This rendered the TreeViews unusable, as it is basically became a simple ListView.
I had to write my own TreeView control from scratch because of this bug... I really hope Microsoft can have this bug fixed.
Steps to reproduce the bug
WinUIItemTemplateSelectorBug.zip
Attached is a test app for this bug. I added a ListView, a GridView and a TreeView to demonstrate this bug. They use a very simple selector based on some very simple data.
Expected behavior
No crash, no broken view.
Screenshots
NuGet package version
WinUI 3 - Windows App SDK 1.4.3: 1.4.231115000
Windows version
Windows Insider Build (xxxxx), Windows 11 (22H2): Build 22621, Windows 11 (21H2): Build 22000, Windows 10 (21H2): Build 19044, Windows 10 (21H1): Build 19043
Additional context
This bug has been there since the first release of WinUI3.
For any one that seek to make your templated TreeView to work without having to write your own treeview control from scratch - there are an extra workarounds things you can try, but it is not granted that it suit your case:
The text was updated successfully, but these errors were encountered: