{"record":{"id":"e96f694527cd2da0","repo":"dotnet/wpf","slug":"sr-itemtemplateselectorbreaksdisplaymemberpath","errorCode":null,"errorMessage":"SR.ItemTemplateSelectorBreaksDisplayMemberPath","messagePattern":"SR\\.ItemTemplateSelectorBreaksDisplayMemberPath","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsControl.cs","lineNumber":654,"sourceCode":"        {\n        }\n\n\n        /// <summary>\n        /// Throw if more than one of DisplayMemberPath, xxxTemplate and xxxTemplateSelector\n        /// properties are set on the given element.\n        /// </summary>\n        private void CheckTemplateSource()\n        {\n            if (string.IsNullOrEmpty(DisplayMemberPath))\n            {\n                Helper.CheckTemplateAndTemplateSelector(\"Item\", ItemTemplateProperty, ItemTemplateSelectorProperty, this);\n            }\n            else\n            {\n                if (!(this.ItemTemplateSelector is DisplayMemberTemplateSelector))\n                {\n                    throw new InvalidOperationException(SR.ItemTemplateSelectorBreaksDisplayMemberPath);\n                }\n                if (Helper.IsTemplateDefined(ItemTemplateProperty, this))\n                {\n                    throw new InvalidOperationException(SR.DisplayMemberPathAndItemTemplateDefined);\n                }\n            }\n        }\n\n        /// <summary>\n        ///     The DependencyProperty for the ItemContainerStyle property.\n        ///     Flags:              none\n        ///     Default Value:      null\n        /// </summary>\n        [CommonDependencyProperty]\n        public static readonly DependencyProperty ItemContainerStyleProperty =\n                DependencyProperty.Register(\n                        \"ItemContainerStyle\",\n                        typeof(Style),","sourceCodeStart":636,"sourceCodeEnd":672,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsControl.cs#L636-L672","documentation":"ItemsControl throws this when a DisplayMemberPath is set and the ItemTemplateSelector is not the special internal DisplayMemberTemplateSelector that respects DisplayMemberPath. Setting a custom template selector alongside DisplayMemberPath would make the two display mechanisms conflict, so WPF rejects it with an InvalidOperationException raised from CheckTemplateSource (via OnItemTemplateChanged/OnItemTemplateSelectorChanged).","triggerScenarios":"Setting DisplayMemberPath on an ItemsControl (ListBox, ComboBox, etc.) and then assigning a custom ItemTemplateSelector (or setting a custom selector first and then DisplayMemberPath). Any selector that is not the library's own DisplayMemberTemplateSelector triggers the throw.","commonSituations":"Developers combine DisplayMemberPath for simple text binding with a custom selector for special-casing some items; XAML copied from samples sets both properties; a style assigns ItemTemplateSelector while DisplayMemberPath remains set from a base style.","solutions":["Remove DisplayMemberPath and express the display text inside your DataTemplates instead.","Remove the custom ItemTemplateSelector if DisplayMemberPath is sufficient.","Wrap custom logic in a DataTemplate chosen by the selector that includes the intended display binding, without using DisplayMemberPath."],"exampleFix":"// before\n<ListBox ItemsSource=\"{Bind Items}\" DisplayMemberPath=\"Name\" ItemTemplateSelector=\"{StaticResource MySelector}\"/>\n// after\n<ListBox ItemsSource=\"{Bind Items}\" ItemTemplateSelector=\"{StaticResource MySelector}\"/>\n<!-- MySelector's templates bind to the Name property directly -->","handlingStrategy":"validation","validationCode":"bool IsDisplayMemberPathCompatible(ItemsControl ic) =>\n    string.IsNullOrEmpty(ic.DisplayMemberPath) || ic.ItemTemplateSelector is null || ic.ItemTemplateSelector is DisplayMemberTemplateSelector;","typeGuard":"bool UsesDisplayMemberTemplateSelector(ItemsControl ic) => ic.ItemTemplateSelector is DisplayMemberTemplateSelector;","tryCatchPattern":"try { ic.DisplayMemberPath = \"Name\"; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"TemplateSelector\")) { ic.ItemTemplateSelector = null; ic.DisplayMemberPath = \"Name\"; }","preventionTips":["Never set DisplayMemberPath and a custom ItemTemplateSelector on the same ItemsControl.","Express per-item display variation entirely in DataTemplates instead of DisplayMemberPath.","Audit based-on styles for inherited ItemTemplateSelector when adding DisplayMemberPath."],"tags":["wpf","itemscontrol","displaymemberpath","itemtemplateselector"],"backgroundTag":"conflicting-config-options","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}