{"record":{"id":"5a023a24907cc35a","repo":"dotnet/wpf","slug":"sr-displaymemberpathanditemtemplatedefined","errorCode":null,"errorMessage":"SR.DisplayMemberPathAndItemTemplateDefined","messagePattern":"SR\\.DisplayMemberPathAndItemTemplateDefined","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsControl.cs","lineNumber":658,"sourceCode":"        /// <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),\n                        typeof(ItemsControl),\n                        new FrameworkPropertyMetadata(\n                                (Style) null,\n                                new PropertyChangedCallback(OnItemContainerStyleChanged)));","sourceCodeStart":640,"sourceCodeEnd":676,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsControl.cs#L640-L676","documentation":"An ItemsControl cannot define both DisplayMemberPath and an explicit ItemTemplate: the template fully controls item rendering, making DisplayMemberPath meaningless. CheckTemplateSource detects the combination (when the selector is the internal DisplayMemberTemplateSelector and Helper.IsTemplateDefined finds an ItemTemplate) and throws InvalidOperationException.","triggerScenarios":"Setting ItemTemplate (or a template arriving via OnItemTemplateChanged) on an ItemsControl that also has DisplayMemberPath set; or setting DisplayMemberPath after an ItemTemplate is already defined.","commonSituations":"Mixing a quick DisplayMemberPath shortcut with a DataTemplate copied from another control; merged resource dictionaries or based-on styles supplying ItemTemplate while XAML sets DisplayMemberPath.","solutions":["Remove DisplayMemberPath and bind the text inside the ItemTemplate.","Remove or rename the ItemTemplate if you only need simple text display.","Use ItemTemplateSelector rather than a hard ItemTemplate if DisplayMemberPath must stay."],"exampleFix":"// before\n<ListBox ItemsSource=\"{Bind Items}\" DisplayMemberPath=\"Name\">\n  <ListBox.ItemTemplate>\n    <DataTemplate><TextBlock Text=\"{Bind Name}\"/></DataTemplate>\n  </ListBox.ItemTemplate>\n</ListBox>\n// after\n<ListBox ItemsSource=\"{Bind Items}\" DisplayMemberPath=\"Name\"/>","handlingStrategy":"validation","validationCode":"bool CanSetItemTemplate(ItemsControl ic) => string.IsNullOrEmpty(ic.DisplayMemberPath) || ic.ItemTemplate is null;","typeGuard":"bool HasBothDisplayMemberAndTemplate(ItemsControl ic) => !string.IsNullOrEmpty(ic.DisplayMemberPath) && ic.ItemTemplate != null;","tryCatchPattern":"try { ic.ItemTemplate = template; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"DisplayMemberPath\")) { ic.DisplayMemberPath = null; ic.ItemTemplate = template; }","preventionTips":["Pick one item-rendering mechanism: DisplayMemberPath OR ItemTemplate.","Check styles/resource dictionaries that may silently provide ItemTemplate.","Bind the display property inside the DataTemplate rather than via DisplayMemberPath."],"tags":["wpf","itemscontrol","displaymemberpath","itemtemplate"],"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-21T21:30:21.729Z"}