DataTemplateSelector - wrong Data Template selected
In my flowlistview, the wrong data template is randomly selected at times.
If I scroll up and down the right data template is sometimes picked. I am trying to display an image for some items and a label for other items.
Details:
I have a data template selector as follows:
protected override DataTemplate OnSelectTemplate(object item, int columnIndex, BindableObject container){
if (item is NormalVideo)
return normalVideoTemplate;
else if (item is LongVideo)
return longVideoTemplate;
else if (item is VideoSummary)
return videoSummaryTemplate;
else if (item is VideoCredit)
return videoCreditTemplate;
NormalVideo, LongVideo, VideoSummary, VideoCredit are all MediaContent. My FlowListView gets a list of MediaContent as its item source. A MediaContent has 2 properties, "Text" and "Image". I want to display the "Image" property for Normal and Long video and "Text" property Video summary and credit with text. Thus normalVideo and longVideo templates only binds to "Image" and videoSummary and videoSummaryTemplate only bind to "Text".
However, items that are NormalVideo keep on getting the videoSummaryTemplate while items that are VideoSummary keep on getting the normalVideoTemplate ...
I have already tried using a common data template that updates its view OnBindingContextChanged but it's much slower (scrolling lags a lot) compared to the DataTemplateSelector.
@fjnoyp I have the very same issue. Did you found any solution for it? It's to bad that @daniel-luberda does not even response to the issues... seems like he does not care...
Hey sorry I took so long to respond.
Unfortunately I did not .. I did a workaround where I had a common view cell class that show/hid views based on the binding context.
So if I was displaying a video object I'd show the imageView and hide the textView, while if I displayed a notebook object I'd hide the imageView and show the textView all within the OnBindingContextChanged overridden method.
Let me know if that helps or you find anything else!
Kyle
On Thu, Feb 22, 2018 at 4:48 AM, Sebastian Kruse [email protected] wrote:
@fjnoyp https://github.com/fjnoyp I have the very same issue. Did you found any solution for it? It's to bad that @daniel-luberda https://github.com/daniel-luberda does not even response to the issues... seems like he does not care...
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/daniel-luberda/DLToolkit.Forms.Controls/issues/166#issuecomment-367670366, or mute the thread https://github.com/notifications/unsubscribe-auth/AK8mgMAZ1JUQqowLtnkmXNkAe-4xFaMkks5tXWIugaJpZM4RiTu3 .
Met the same issue - a likely cause is how the underlying ListView handles the combination of CachingStrategy and DataTemplateSelector.
The default CachingStrategy is Recycle, which doesn't consider the DataTemplate applied to the cell (each cell instance keeps the DataTemplate it was first created with).
So either make sure not to use a Selector with Recycle cell caching, or look at using the RecycleElementAndDataTemplate which should handle this (I didn't end up trying this, solved by removing the Selector and making my DataTemplate more flexible with ValueConverters).
Complete explanation here: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/performance#caching-strategy
Any ideas how could we solve it?
@daniel-luberda Haven't used a ListView in a while, but re-reading the page I linked it looks like this might be solved in XF 2.4: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/performance#recycleelement-with-a-datatemplateselector
RecycleElementAndDataTemplate looks like it could also be a useful option when using different cell types / datatemplates. Might give better performance as well since the datatemplates are cached?