stride3d/stride · error · ArgumentException

Container must be of type FrameworkElement

Error message

Container must be of type FrameworkElement

What it means

As an IValueConverter-based template selector, SelectTemplate receives the container the selector is applied to. It requires that container to be a FrameworkElement (needed to locate templates and bind to them) and throws ArgumentException when the cast fails.

Solutions

  1. Ensure the selector is attached where the container is a FrameworkElement (e.g. ContentControl.ContentTemplateSelector)
  2. If calling directly, pass the actual FrameworkElement container
  3. Wrap container selection in your own selector that guards the cast and falls back gracefully

Example fix

// before
selector.SelectTemplate(item, somePlainObject); // throws
// after
if (somePlainObject is FrameworkElement fe)
    template = selector.SelectTemplate(item, fe);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(container is FrameworkElement)) throw new ArgumentException("container must be FrameworkElement");

Type guard

bool ValidContainer(object c) => c is FrameworkElement;

Try / catch

try { t = selector.SelectTemplate(item, container); } catch (ArgumentException ex) when (ex.Message.Contains("FrameworkElement")) { t = null; }

Prevention

When it happens

Trigger: Applying the selector through a container that is not a FrameworkElement — e.g. a ContentPresenter-only path, a DataTemplateSelector registered in an unsupported position, or calling SelectTemplate directly with an arbitrary object.

Common situations: Using the selector on items controls whose containers are generated as non-FrameworkElement types; misuse in unit tests passing a plain object as container; framework version changes altering container types.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/b995bb78f5ae2118. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation.Wpf/View/TemplateProviderSelector.cs:75

        /// Unregisters the given template into the static <see cref="TemplateProviderSelector"/>.
        /// </summary>
        /// <param name="templateProvider"></param>
        public void UnregisterTemplateProvider([NotNull] ITemplateProvider templateProvider)
        {
            if (templateProviderNames.Remove(templateProvider.Name))
            {
                templateProviders.Remove(templateProvider);
            }
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item == null)
                return null;

            var element = container as FrameworkElement;
            if (element == null)
                throw new ArgumentException(@"Container must be of type FrameworkElement", nameof(container));

            var provider = FindTemplateProvider(item, container);
            if (provider == null)
                return null;

            var template = provider.Template;
            // We set the template we found into the content presenter itself to avoid re-entering the template selector if the property is refreshed.
            //var contentPresenter = container as ContentPresenter;
            //if (contentPresenter != null)
            //{
            //    contentPresenter.ContentTemplate = template;
            //}
            return template;
        }
        
        private static void InsertTemplateProvider([NotNull] List<ITemplateProvider> list, ITemplateProvider templateProvider, [NotNull] List<ITemplateProvider> movedItems)
        {
            movedItems.Add(templateProvider);

View on GitHub (pinned to 96fad776d2)