{"record":{"id":"07c2005ea90ba7b5","repo":"dotnet/wpf","slug":"sr-displaymemberpathanditemtemplateselectordefined","errorCode":null,"errorMessage":"SR.DisplayMemberPathAndItemTemplateSelectorDefined","messagePattern":"SR\\.DisplayMemberPathAndItemTemplateSelectorDefined","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsControl.cs","lineNumber":412,"sourceCode":"        {\n            string displayMemberPath = DisplayMemberPath;\n            string itemStringFormat = ItemStringFormat;\n\n            if (!String.IsNullOrEmpty(displayMemberPath) || !String.IsNullOrEmpty(itemStringFormat))\n            {\n                // One or both of DisplayMemberPath and ItemStringFormat are desired.\n                // Set ItemTemplateSelector to an appropriate object, provided that\n                // this doesn't conflict with the user's own setting.\n                DataTemplateSelector itemTemplateSelector = ItemTemplateSelector;\n                if (itemTemplateSelector != null && !(itemTemplateSelector is DisplayMemberTemplateSelector))\n                {\n                    // if ITS was actually set to something besides a DisplayMember selector,\n                    // it's an error to overwrite it with a DisplayMember selector\n                    // unless ITS came from a style and DMP is local\n                    if (ReadLocalValue(ItemTemplateSelectorProperty) != DependencyProperty.UnsetValue ||\n                        ReadLocalValue(DisplayMemberPathProperty) == DependencyProperty.UnsetValue)\n                    {\n                        throw new InvalidOperationException(SR.DisplayMemberPathAndItemTemplateSelectorDefined);\n                    }\n                }\n\n                // now set the ItemTemplateSelector to use the new DisplayMemberPath and ItemStringFormat\n                ItemTemplateSelector = new DisplayMemberTemplateSelector(DisplayMemberPath, ItemStringFormat);\n            }\n            else\n            {\n                // Neither property is desired.  Clear the ItemTemplateSelector if\n                // we had set it earlier.\n                if (ItemTemplateSelector is DisplayMemberTemplateSelector)\n                {\n                    ClearValue(ItemTemplateSelectorProperty);\n                }\n            }\n        }\n\n        /// <summary>","sourceCodeStart":394,"sourceCodeEnd":430,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsControl.cs#L394-L430","documentation":"ItemsControl throws InvalidOperationException (SR.DisplayMemberPathAndItemTemplateSelectorDefined) when DisplayMemberPath (or ItemStringFormat) changes and an ItemTemplateSelector that is not the internal DisplayMemberTemplateSelector is already locally set. DisplayMemberPath works by installing its own template selector, so defining both an explicit ItemTemplateSelector and a DisplayMemberPath is ambiguous and forbidden.","triggerScenarios":"Setting ItemsControl.DisplayMemberPath (or ItemStringFormat) in code after having locally assigned ItemTemplateSelector, or vice versa; setting DisplayMemberPath when ItemTemplateSelector was set in a style while DisplayMemberPath is not a local value.","commonSituations":"XAML with both DisplayMemberPath=\"Name\" and ItemTemplateSelector set on the same ItemsControl; code-behind assigning ItemTemplateSelector after the designer set DisplayMemberPath; styling an ItemsControl with a template selector while the app sets DisplayMemberPath locally.","solutions":["Choose one mechanism: either DisplayMemberPath (simple text display) or ItemTemplateSelector (custom logic), not both.","If you need custom templates, clear DisplayMemberPath (ItemsControl.DisplayMemberPath = String.Empty) and use DataTemplateSelector alone.","Use a DataTemplate with a binding to the property formerly named in DisplayMemberPath instead.","If the selector must co-exist with DisplayMemberPath semantics, fold the DisplayMemberPath logic into your own DisplayMemberTemplateSelector-equivalent."],"exampleFix":"// before\nlistBox.DisplayMemberPath = \"Name\"; // throws: ItemTemplateSelector already set locally\n\n// after\nlistBox.DisplayMemberPath = String.Empty; // drop DMP, rely on the selector\n// or remove: listBox.ItemTemplateSelector = null; before setting DisplayMemberPath","handlingStrategy":"type-guard","validationCode":"static bool CanSetDisplayMemberPath(ItemsControl c) =>\n    (c.ItemTemplateSelector is DisplayMemberTemplateSelector || c.ItemTemplateSelector == null) ||\n    c.ItemTemplateSelector == null;","typeGuard":"bool SafeToUseDisplayMemberPath(ItemsControl c) =>\n    c.ReadLocalValue(ItemsControl.ItemTemplateSelectorProperty) == DependencyProperty.UnsetValue\n    || c.ItemTemplateSelector == null;","tryCatchPattern":"try { itemsControl.DisplayMemberPath = \"Name\"; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"DisplayMemberPath\"))\n{\n    itemsControl.DisplayMemberPath = String.Empty; // clear conflicting setting\n    itemsControl.DisplayMemberPath = \"Name\";\n}","preventionTips":["Pick DisplayMemberPath OR ItemTemplateSelector per control, never both","Check ReadLocalValue(ItemTemplateSelectorProperty) before assigning DisplayMemberPath","Search XAML for controls that declare both attributes","Centralize template selection in one DataTemplateSelector and avoid DisplayMemberPath entirely"],"tags":["wpf","itemscontrol","displaymemberpath","datatemplate-selector"],"backgroundTag":"mutually-exclusive-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"}