Bug 56084 - Changing DataTemplateSelector Causes Invalid Value ArgumentException
Summary: Changing DataTemplateSelector Causes Invalid Value ArgumentException
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.4
Hardware: All All
: Normal major
Target Milestone: ---
Assignee: Chris King
Depends on:
Reported: 2017-05-07 20:30 UTC by Hines Vaughan III
Modified: 2017-05-11 13:51 UTC (History)
5 users (show)

Tags: datatemplateselector exception ac
Is this bug a regression?: ---
Last known good build:

Solution demonstrating ArgumentException With Changing The DataTemplateSelector (272.23 KB, application/zip)
2017-05-07 20:30 UTC, Hines Vaughan III

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Hines Vaughan III 2017-05-07 20:30:29 UTC
Created attachment 21994 [details]
Solution demonstrating ArgumentException With Changing The DataTemplateSelector

Running into an exception happening when using DataTemplateSelectors but not happening when doing the same thing with a regular DataTemplate.

I have a ListView in my real app that shows a list of items. When the user taps a filter button, I filter all of the items, by price for example, but I also need to completely change the layout of of the ViewCell for each item when the filter changes. To do this, I have been changing the bound DataTemplate on the ListView.ItemTemplate using a new DateTemplate containing a different ViewCell so all of the items use a completely new layout.

I wanted to do the same thing with DataTemplateSelectors but when ever I change the bound DataTemplateSelector on my ListView.ItemTemplate I receive the exception below.

I tried only changing the DataTemplateSelector's DataTemplate property, which should mimic change the DataTemplate of the ListView directly, but it generated the same exception, which makes me think the issue is with the DataTemplateSelector and might be something that actually should be working.

I plan to use an enum contained in my model and not change the DataTemplateSelector but thought it might be helpful to create this bug report anyway. Please feel free to close it if this is by design and I am doing something that I shouldn't be.

Below is version info, info about the attached solution, exception stack trace, and my IDE version info. Let me know if anything else is needed.


Device/Library Version Info:

This bug was found on an iPhone 8.4 Simulator and an Android 7.1 Genymotion Emulator.

The bug was found using Xamarin.Forms. I also tried downloading the latest nightly build, Xamarin.Forms., and the same exception occurred.


Solution Info:

Attached is a solution demonstrating the issue. It is automatically set to use the problem page, called 'MainPageBrokenSelector'. In order to see the working example using a regular DataTemplate, I also included the 'MainPageWorkingTemplate'.



Unhandled Exception:
	System.ArgumentException: Value was an invalid value for ItemTemplate
	Parameter name: value
	  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00088] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:357 
	  at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x001f9] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindingExpression.cs:173 
	  at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindingExpression.cs:55 
	  at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__47_0 () [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindingExpression.cs:539 
	  at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/4466/a04678c2/source/xamarin-macios/src/Foundation/NSAction.cs:163 
	--- End of stack trace from previous location where exception was thrown ---
	  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
	  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/4466/a04678c2/source/xamarin-macios/src/UIKit/UIApplication.cs:79 
	  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/4466/a04678c2/source/xamarin-macios/src/UIKit/UIApplication.cs:63 
	  at DataTempSelectExample.iOS.Application.Main (System.String[] args) [0x00001] in C:\Users\hvaughan3\Documents\DataTempSelectExample\DataTempSelectExample\DataTempSelectExample\DataTempSelectExample.iOS\Main.cs:9


IDE Version Info:

Microsoft Visual Studio Professional 2015
Version 14.0.25431.01 Update 3
Microsoft .NET Framework
Version 4.6.01055

Installed Version: Professional

LightSwitch for Visual Studio 2015   00322-40000-00000-AA625
Microsoft LightSwitch for Visual Studio 2015

Microsoft Visual Studio Tools for Applications 2015   00322-40000-00000-AA625
Microsoft Visual Studio Tools for Applications 2015

Visual Basic 2015   00322-40000-00000-AA625
Microsoft Visual Basic 2015

Visual C# 2015   00322-40000-00000-AA625
Microsoft Visual C# 2015

Visual C++ 2015   00322-40000-00000-AA625
Microsoft Visual C++ 2015

Windows Phone SDK 8.0 - ENU   00322-40000-00000-AA625
Windows Phone SDK 8.0 - ENU

ActiveReports 10   10.2.8028.0
The first report writer for Microsoft .NET

ASP.NET and Web Tools 2015.1   14.1.21111.0
ASP.NET and Web Tools 2015.1

ASP.NET Web Frameworks and Tools 2012.2   4.1.41102.0
For additional information, visit http://go.microsoft.com/fwlink/?LinkID=309563

ASP.NET Web Frameworks and Tools 2013   5.2.40314.0
For additional information, visit http://www.asp.net/

Azure App Service Tools v2.9.6   14.0.21111.0
Azure App Service Tools v2.9.6

Azure Data Lake Node   1.0
This package contains the Data Lake integration nodes for Server Explorer.

Azure Data Lake Tools for Visual Studio   2.2.2100.0
Microsoft Azure Data Lake Tools for Visual Studio

Common Azure Tools   1.8
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

JavaScript Language Service   2.0
JavaScript Language Service

JavaScript Project System   2.0
JavaScript Project System

JetBrains ReSharper Ultimate 2017.1.2    Build 108.0.20170428.75743
JetBrains ReSharper Ultimate package for Microsoft Visual Studio. For more information about ReSharper Ultimate, visit http://www.jetbrains.com/resharper. Copyright © 2017 JetBrains, Inc.

Merq   1.1.17-rc (cba4571)
Command Bus, Event Stream and Async Manager for Visual Studio extensions.

Microsoft .NET Core Tools (Preview 2)   14.1.21111.0
Microsoft .NET Core Tools (Preview 2)

Microsoft Azure Hive Query Language Service   2.2.2100.0
Language service for Hive query

Microsoft Azure Mobile Services Tools   1.4
Microsoft Azure Mobile Services Tools

Microsoft Azure Tools   2.9
Microsoft Azure Tools for Microsoft Visual Studio 2015 - v2.9.41104.6

Mono Debugging for Visual Studio   Mono.Debugging.VisualStudio
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   3.5.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SQL Server Analysis Services   13.0.1701.8
Microsoft SQL Server Analysis Services Designer 
Version 13.0.1701.8

SQL Server Data Tools   14.0.61021.0
Microsoft SQL Server Data Tools

SQL Server Integration Services   
Microsoft SQL Server Integration Services Designer
Version 13.0.1601.5

SQL Server Reporting Services   13.0.1701.8
Microsoft SQL Server Reporting Services Designers 
Version 13.0.1701.8

ToolWindowHostedEditor   1.0
Hosting json editor into a tool window

Trailing Whitespace Visualizer   2.5.83
Keeps your code files clean by making it easier than ever to identify and remove any trailing whitespace

TypeScript tools for Visual Studio

Visual Studio Spell Check Everywhere   VSSpellCheckEverywhere
An extension that enables spell checking within any Visual Studio file editor or tool window that uses WPF text boxes.

Visual Studio Spell Checker   VSSpellChecker
An editor extension that checks the spelling of comments, strings, and plain text as you type or interactively with tool windows.

Xamarin (3f99c5a)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android (b16fb82)
Visual Studio extension to enable development for Xamarin.Android.

Xamarin.iOS (7656cc6)
Visual Studio extension to enable development for Xamarin.iOS.
Comment 1 Paul DiPietro [MSFT] 2017-05-08 19:00:38 UTC
Thank you for the reproduction. Setting to confirmed until this can be further investigated.
Comment 2 Chris King 2017-05-09 15:42:41 UTC
The exception is thrown because the ValidateItemTemplate returns false. See: https://github.com/xamarin/Xamarin.Forms/blob/ba06e2b20dbbb82dafacf63dd3e3e1810d976a56/Xamarin.Forms.Core/ItemsView.cs#L96

Basically, you need to enabled RecycleElement when using a DataTemplateSelector by adding CachingStrategy="RecycleElement" to your ListView XAML. See also: https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/performance/

The exception should be clearer but the validation mechanism doesn't currently support custom messages on failure. We'll look at that.
Comment 3 Hines Vaughan III 2017-05-09 15:54:11 UTC

I see, thank you for the help.

So would it be correct for the ListView to require RecycleElement only when changing the DataTemplateSelector and would not require it when I first initialize the ListView with a DataTemplateSelector?
Comment 4 Chris King 2017-05-11 13:51:34 UTC
If a ListView uses a DataTemplateSelector then RecycleElement should be true. You may be able to defer setting it to true for some period but we'd suggest eagerly setting it true if you're going to use a DataTempateSelector. 

Please direct further questions to SO or forums and reserve bugzilla issues for suspected bugs. If no one there answers your question in a timely fashion then feel free to file a bugzilla issue as a last resort.