Search code examples
xamarin.formsviewcarouseldatatemplatecontentview

Xamarin Forms, Adding items dynamically inside CarouselView on the run, Strange Behaviour


I use the same DataTemplate to populate some of the views inside my CarouselView: a XAML file with a ContentView. I assign a custom class list to ItemsSource of the carouselview AND use bindings inside the template layout fields to fill them with the properties of my custom class. Everything is working fine. Here comes the wierd part: Let's say there's a button in the template and when you click it on the screen, a Label with "Well Done" text is added to the current Stacklayout of the shown carouselview. This is done by the codebehind of my template XAML. This is ok, too, but I expect this "Well Done" to disappear after I swipe back and forth enough. Because it's not part of the template. It does not disappear, instead I see it on other carousels once in every 4 swipes and it is replaced, I mean it appears on different carousels if I swipe back and forth. To make it disappear completely I have to swipe away from the views that share the same DataTemplate. I cannot understand the mechanics behind this behaviour. I can create another field inside the template to put the "Well Done" text there and show it when the time comes, I know. But I want to understand the mechanics. Thank you.


Solution

  • Asking the right question about a problem is the key to any solution.. After the realization that I've mentioned at the Update comment, I've searched for DataTemplate Caching and found this issue post on Xamarin.Forms Github: https://github.com/xamarin/Xamarin.Forms/issues/9200

    There you can find an explanation to how CollectionView was designed and understand the mechanics behind the behaviour. No bug. It's by design.