dotnet/maui · error · InvalidOperationException

Cannot call CreateContent directly on a DataTemplateSelector

Error message

Cannot call CreateContent directly on a DataTemplateSelector

What it means

Thrown when CreateContent() is called directly on a DataTemplateSelector instance. A DataTemplateSelector is abstract and must first pick a concrete DataTemplate via SelectTemplate(item, container) based on the bound item; it has no single template to instantiate. The guards in ElementTemplate.cs:84 block the parameterless path because it would be ambiguous which child template to materialize.

Source

Thrown at src/Controls/src/Core/ElementTemplate.cs:85

		{
			if (_changeHandlers == null)
				return;
			_changeHandlers.Remove(onchanged);
		}

		/// <summary>Used by the XAML infrastructure to load data templates and set up the content of the resulting UI.</summary>
		public object CreateContent()
		{
			if (LoadTemplate == null)
			{
				// Returning a Label here instead of throwing an exception because HotReload may temporarily be in state
				// where the user is creating a template; this keeps everything else (which expects a result from CreateContent)
				// from crashing during that time. 
				return new Label();
			}

			if (this is DataTemplateSelector)
				throw new InvalidOperationException("Cannot call CreateContent directly on a DataTemplateSelector");

			object item = LoadTemplate();
			SetupContent(item);

			if (item is Element elem)
				elem.IsTemplateRoot = true;

			return item;
		}

		internal virtual void SetupContent(object item)
		{
		}

		void OnResourcesChanged(object sender, ResourcesChangedEventArgs e)
		{
			if (_changeHandlers == null)
				return;

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Use the DataTemplateExtensions.CreateContent(this DataTemplate, item, container) overload (DataTemplateExtensions.cs:29), which internally calls SelectDataTemplate then CreateContent.
  2. If you hold the selector explicitly, call selector.SelectTemplate(item, container).CreateContent() instead.
  3. For controls like CollectionView/ListView, assign the selector to the *Selector property (e.g. ItemTemplateSelector) rather than ItemTemplate, so the control drives selection.
  4. Guard with `if (template is DataTemplateSelector s) { ... s.SelectTemplate(...) ... } else { template.CreateContent(); }` when you must branch.

Example fix

// before
DataTemplate template = GetTemplate();
var view = (View)template.CreateContent(); // throws if template is a selector

// after
using Microsoft.Maui.Controls.Internals;
var view = (View)template.CreateContent(item, container); // routes through SelectTemplate
Defensive patterns

Strategy: type-guard

Validate before calling

if (template is DataTemplateSelector selector) { return selector.SelectTemplate(item, container); } return template.CreateContent();

Type guard

static bool IsSelector(DataTemplate t) => t is DataTemplateSelector;

Try / catch

try { return template.CreateContent(item, container); } catch (InvalidOperationException) when (template is DataTemplateSelector) { /* use SelectTemplate path */ }

Prevention

When it happens

Trigger: Calling the parameterless `selector.CreateContent()` on an instance whose runtime type derives from DataTemplateSelector. This happens when generic code holds a `DataTemplate` reference but the object is actually a selector, and it invokes the zero-arg CreateContent rather than the SelectTemplate path.

Common situations: Custom layout adapters, cell factories, or test code that receives a DataTemplate and unconditionally calls CreateContent() without first narrowing. Migrating a fixed ItemTemplate to an ItemTemplateSelector without updating the consumption site. Tooling that introspects templates generically.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/34a253fb0be36894. Report an issue: GitHub.