maui icon indicating copy to clipboard operation
maui copied to clipboard

DataTemplateSelector on CollectionView is completely broken - templates don't match BindingContext

Open Vroomer opened this issue 2 years ago • 7 comments

Description

Items in CollectionView utilizing DataTemplateSelector get often assigned to wrong DataTemplate. It appears to be a problem related to recycling views. While first items are usually displayed with correct DataTemplate, once the recycling kicks in the DataTemplates are mismatched. It's a serious issue as it makes the DataTemplateSelector unusable without any workaround. The problem was observed on Windows. Other platforms were not tested.

It might be the same problem as https://github.com/dotnet/maui/issues/11272. There is also a related PR to this issue right now https://github.com/dotnet/maui/pull/12011, but both the issue and PR mention only Android and iOS and the description of the problem is different. In my case the views get recycled, but incorrectly.

I made a sample showing list of colors to visualize the problem. Each row consists of two colors - one color assigned by DataTemplate and the other assigned by BindingContext.

Screenshot 2023-01-30 000442

Steps to Reproduce

  1. Load the repository and run the app.
  2. Scroll the CollectionView to trigger virtualization.
  3. Notice mismatched colors. Left half is the color assigned by DataTemplate, right half is color from BindingContext. Differences in numbers can help identify recycled views.

Link to public reproduction project repository

https://github.com/Vroomer/MAUI-CollectionView-DataTemplateSelector-Bug.git

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Windows, I was not able test on other platforms

Affected platform versions

Windows SDK 10.0.22621

Did you find any workaround?

No response

Relevant log output

No response

Vroomer avatar Jan 29 '23 23:01 Vroomer

Related or the same as https://github.com/dotnet/maui/issues/11272

jsuarezruiz avatar Jan 30 '23 15:01 jsuarezruiz

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Jan 30 '23 15:01 ghost

Hi @Vroomer. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Jun 13 '23 09:06 ghost

Verified this issue with Visual Studio Enterprise 17.7.0 Preview 1.0. Not repro on Windows platform with sample project. MAUI-CollectionView-DataTemplateSelector-Bug.git

homeyf avatar Jun 13 '23 09:06 homeyf

image image @homeyf which version? I can still reproduce it in main branch 7.0.0-ci.net7.18626

Vroomer avatar Jun 15 '23 11:06 Vroomer

I also have found this an issue (8.2.1) - I have ~130 entries to enumerate, though the DataTemplateSelector only gets called 25 times, then it appears the remaining entries are still mapped to the first 25 and no more calls are made.

Eitsop avatar Aug 11 '23 10:08 Eitsop

I am also facing this issue. Collectionview control is not usable with this bug if we have to vertically scroll. I am trying to convert the existing xamaring app to maui and stuck with this. cant migrate it without this bug is fixed.

Update: It seems to be that if the template has control that is data bound then it works. If the controls is built on the fly with the bindingcontext values it does not work. i was able to solve my problem by changing the datatemplate controls to be data bound rather than building with the controls to use values from bindingcontext.

last-Programmer avatar Dec 21 '23 09:12 last-Programmer