PrismLibrary/Prism · error · InvalidOperationException

Resources.ItemsControlHasItemsSourceException

Error message

Resources.ItemsControlHasItemsSourceException

What it means

SelectorItemsSourceSyncBehavior.OnAttach validates that the region's host control (an ItemsControl/Selector) does not already have an ItemsSource or an ItemsSource binding, throwing InvalidOperationException with Resources.ItemsControlHasItemsSourceException otherwise. The behavior must own ItemsSource to synchronize it with the region's views.

Solutions

  1. Remove the ItemsSource assignment/binding from the host control and let the region populate it (add views via region.Add or RegisterViewWithRegion).
  2. If data-binding is required, use the control directly without RegionName, or host the region inside a separate container control.
  3. Move the data to a view-model and register a view that renders it inside the region.
  4. Check style/implicit bindings — HasBinding also catches ItemsSource set through styles/templates.

Example fix

// before
<ListBox ItemsSource="{Binding Items}" prism:RegionManager.RegionName="ListRegion" />
// after
<ListBox prism:RegionManager.RegionName="ListRegion" />
Defensive patterns

Strategy: validation

Validate before calling

if (hostControl.ItemsSource != null || hostControl.HasBinding(ItemsControl.ItemsSourceProperty))
    throw new InvalidOperationException("Remove ItemsSource before attaching a region to this control.");

Type guard

bool IsRegionCompatible(ItemsControl c) => c.ItemsSource == null && !c.HasBinding(ItemsControl.ItemsSourceProperty);

Try / catch

try
{
    RegionManager.SetRegionName(listBox, "ListRegion");
}
catch (InvalidOperationException ex) when (ex.Message.Contains("ItemsSource"))
{
    logger.LogError(ex, "Control has ItemsSource set; region cannot take ownership.");
}

Prevention

When it happens

Trigger: Attaching a region to a control (via RegionManager.RegionName) that already has ItemsSource set in XAML or code, or that has a binding on ItemsControl.ItemsSourceProperty.

Common situations: Developers binding a ListBox's ItemsSource in XAML and then adding RegionName to the same control; MVVM code assigning ItemsSource in the view-model-connected code-behind; converting an existing data-bound list into a region.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/72af5895451347fd. Report an issue: GitHub.

Appendix: source

Thrown at src/Wpf/Prism.Wpf/Navigation/Regions/Behaviors/SelectorItemsSourceSyncBehavior.cs:55

            }

            set
            {
                _hostControl = value as Selector;
            }
        }

        /// <summary>
        /// Starts to monitor the <see cref="IRegion"/> to keep it in sync with the items of the <see cref="HostControl"/>.
        /// </summary>
        protected override void OnAttach()
        {
            bool itemsSourceIsSet = _hostControl.ItemsSource != null;
            itemsSourceIsSet = itemsSourceIsSet || _hostControl.HasBinding(ItemsControl.ItemsSourceProperty);

            if (itemsSourceIsSet)
            {
                throw new InvalidOperationException(Resources.ItemsControlHasItemsSourceException);
            }

            SynchronizeItems();

            _hostControl.SelectionChanged += HostControlSelectionChanged;
            Region.ActiveViews.CollectionChanged += ActiveViews_CollectionChanged;
            Region.Views.CollectionChanged += Views_CollectionChanged;
        }

        private void Views_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                int startIndex = e.NewStartingIndex;
                foreach (object newItem in e.NewItems)
                {
                    _hostControl.Items.Insert(startIndex++, newItem);
                }

View on GitHub (pinned to 358118cd64)