dotnet/wpf · error · InvalidOperationException

SR.DisplayMemberPathAndItemTemplateDefined

Error message

SR.DisplayMemberPathAndItemTemplateDefined

What it means

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.

Solutions

  1. Remove DisplayMemberPath and bind the text inside the ItemTemplate.
  2. Remove or rename the ItemTemplate if you only need simple text display.
  3. Use ItemTemplateSelector rather than a hard ItemTemplate if DisplayMemberPath must stay.

Example fix

// before
<ListBox ItemsSource="{Bind Items}" DisplayMemberPath="Name">
  <ListBox.ItemTemplate>
    <DataTemplate><TextBlock Text="{Bind Name}"/></DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>
// after
<ListBox ItemsSource="{Bind Items}" DisplayMemberPath="Name"/>
Defensive patterns

Strategy: validation

Validate before calling

bool CanSetItemTemplate(ItemsControl ic) => string.IsNullOrEmpty(ic.DisplayMemberPath) || ic.ItemTemplate is null;

Type guard

bool HasBothDisplayMemberAndTemplate(ItemsControl ic) => !string.IsNullOrEmpty(ic.DisplayMemberPath) && ic.ItemTemplate != null;

Try / catch

try { ic.ItemTemplate = template; } catch (InvalidOperationException ex) when (ex.Message.Contains("DisplayMemberPath")) { ic.DisplayMemberPath = null; ic.ItemTemplate = template; }

Prevention

When it happens

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

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

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/5a023a24907cc35a. Report an issue: GitHub.

Appendix: source

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

        /// <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),
                        typeof(ItemsControl),
                        new FrameworkPropertyMetadata(
                                (Style) null,
                                new PropertyChangedCallback(OnItemContainerStyleChanged)));

View on GitHub (pinned to 81131a70a4)