dotnet/wpf · error · InvalidOperationException

SR.ItemTemplateSelectorBreaksDisplayMemberPath

Error message

SR.ItemTemplateSelectorBreaksDisplayMemberPath

What it means

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).

Solutions

  1. Remove DisplayMemberPath and express the display text inside your DataTemplates instead.
  2. Remove the custom ItemTemplateSelector if DisplayMemberPath is sufficient.
  3. Wrap custom logic in a DataTemplate chosen by the selector that includes the intended display binding, without using DisplayMemberPath.

Example fix

// before
<ListBox ItemsSource="{Bind Items}" DisplayMemberPath="Name" ItemTemplateSelector="{StaticResource MySelector}"/>
// after
<ListBox ItemsSource="{Bind Items}" ItemTemplateSelector="{StaticResource MySelector}"/>
<!-- MySelector's templates bind to the Name property directly -->
Defensive patterns

Strategy: validation

Validate before calling

bool IsDisplayMemberPathCompatible(ItemsControl ic) =>
    string.IsNullOrEmpty(ic.DisplayMemberPath) || ic.ItemTemplateSelector is null || ic.ItemTemplateSelector is DisplayMemberTemplateSelector;

Type guard

bool UsesDisplayMemberTemplateSelector(ItemsControl ic) => ic.ItemTemplateSelector is DisplayMemberTemplateSelector;

Try / catch

try { ic.DisplayMemberPath = "Name"; } catch (InvalidOperationException ex) when (ex.Message.Contains("TemplateSelector")) { ic.ItemTemplateSelector = null; ic.DisplayMemberPath = "Name"; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/e96f694527cd2da0. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsControl.cs:654

        {
        }


        /// <summary>
        /// Throw if more than one of DisplayMemberPath, xxxTemplate and xxxTemplateSelector
        /// properties are set on the given element.
        /// </summary>
        private void CheckTemplateSource()
        {
            if (string.IsNullOrEmpty(DisplayMemberPath))
            {
                Helper.CheckTemplateAndTemplateSelector("Item", ItemTemplateProperty, ItemTemplateSelectorProperty, this);
            }
            else
            {
                if (!(this.ItemTemplateSelector is DisplayMemberTemplateSelector))
                {
                    throw new InvalidOperationException(SR.ItemTemplateSelectorBreaksDisplayMemberPath);
                }
                if (Helper.IsTemplateDefined(ItemTemplateProperty, this))
                {
                    throw new InvalidOperationException(SR.DisplayMemberPathAndItemTemplateDefined);
                }
            }
        }

        /// <summary>
        ///     The DependencyProperty for the ItemContainerStyle property.
        ///     Flags:              none
        ///     Default Value:      null
        /// </summary>
        [CommonDependencyProperty]
        public static readonly DependencyProperty ItemContainerStyleProperty =
                DependencyProperty.Register(
                        "ItemContainerStyle",
                        typeof(Style),

View on GitHub (pinned to 81131a70a4)