{"record":{"id":"b995bb78f5ae2118","repo":"stride3d/stride","slug":"container-must-be-of-type-frameworkelement","errorCode":null,"errorMessage":"Container must be of type FrameworkElement","messagePattern":"Container must be of type FrameworkElement","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/View/TemplateProviderSelector.cs","lineNumber":75,"sourceCode":"        /// Unregisters the given template into the static <see cref=\"TemplateProviderSelector\"/>.\n        /// </summary>\n        /// <param name=\"templateProvider\"></param>\n        public void UnregisterTemplateProvider([NotNull] ITemplateProvider templateProvider)\n        {\n            if (templateProviderNames.Remove(templateProvider.Name))\n            {\n                templateProviders.Remove(templateProvider);\n            }\n        }\n\n        public override DataTemplate SelectTemplate(object item, DependencyObject container)\n        {\n            if (item == null)\n                return null;\n\n            var element = container as FrameworkElement;\n            if (element == null)\n                throw new ArgumentException(@\"Container must be of type FrameworkElement\", nameof(container));\n\n            var provider = FindTemplateProvider(item, container);\n            if (provider == null)\n                return null;\n\n            var template = provider.Template;\n            // We set the template we found into the content presenter itself to avoid re-entering the template selector if the property is refreshed.\n            //var contentPresenter = container as ContentPresenter;\n            //if (contentPresenter != null)\n            //{\n            //    contentPresenter.ContentTemplate = template;\n            //}\n            return template;\n        }\n        \n        private static void InsertTemplateProvider([NotNull] List<ITemplateProvider> list, ITemplateProvider templateProvider, [NotNull] List<ITemplateProvider> movedItems)\n        {\n            movedItems.Add(templateProvider);","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/View/TemplateProviderSelector.cs#L57-L93","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the selector is attached where the container is a FrameworkElement (e.g. ContentControl.ContentTemplateSelector)","If calling directly, pass the actual FrameworkElement container","Wrap container selection in your own selector that guards the cast and falls back gracefully"],"exampleFix":"// before\nselector.SelectTemplate(item, somePlainObject); // throws\n// after\nif (somePlainObject is FrameworkElement fe)\n    template = selector.SelectTemplate(item, fe);","handlingStrategy":"type-guard","validationCode":"if (!(container is FrameworkElement)) throw new ArgumentException(\"container must be FrameworkElement\");","typeGuard":"bool ValidContainer(object c) => c is FrameworkElement;","tryCatchPattern":"try { t = selector.SelectTemplate(item, container); } catch (ArgumentException ex) when (ex.Message.Contains(\"FrameworkElement\")) { t = null; }","preventionTips":["Attach selectors only where containers are FrameworkElements","Guard direct calls in tests","Confirm ContentTemplateSelector usage rather than unsupported positions"],"tags":["wpf","argument","template-selector"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}