dotnet/wpf · error · InvalidOperationException

SR.ItemsSourceInUse

Error message

SR.ItemsSourceInUse

What it means

ItemCollection.Clear throws InvalidOperationException (SR.ItemsSourceInUse) when the collection is backed by an ItemsSource (IsUsingItemsSource). In that mode the ItemsControl delegates storage to the source collection, so direct mutation of ItemCollection is not allowed. The comment notes it deliberately avoids creating the internal view and also verifies refresh is not deferred.

Solutions

  1. Clear the underlying source collection instead of Items (e.g. ObservableCollection.Clear)
  2. Set ItemsSource = null before mutating Items directly
  3. Choose one mode: either bind via ItemsSource or populate Items manually, not both

Example fix

// before
listBox.Items.Clear(); // ItemsSource is set
// after
var oc = listBox.ItemsSource as ObservableCollection<Item>;
oc?.Clear();
Defensive patterns

Strategy: validation

Validate before calling

bool canClearDirectly = !(items as ItemCollection)?.IsUsingItemsSource ?? true; // mutate source collection if bound

Type guard

bool IsBound(ItemCollection c) => c.SourceCollection != c; // SourceCollection differs when ItemsSource is in use

Try / catch

try { itemsControl.Items.Clear(); } catch (InvalidOperationException ex) when (ex.Message.Contains("ItemsSource")) { (itemsControl.ItemsSource as System.Collections.IList)?.Clear(); }

Prevention

When it happens

Trigger: Calling items.Clear() (or other direct mutations) on an ItemsControl's Items collection while ItemsSource is set. Any manipulation of Items (Add/Remove/Clear) with ItemsSource assigned throws this family of errors.

Common situations: Setting ItemsSource in XAML/data binding but also trying to clear/seed Items in code-behind; switching between manual items and bound mode without clearing ItemsSource first.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs:246

            return index;
        }

        /// <summary>
        ///     Clears the collection.  Releases the references on all items
        /// currently in the collection.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// the ItemCollection is read-only because it is in ItemsSource mode
        /// </exception>
        public void Clear()
        {
            // Not using CheckIsUsingInnerView() because we don't want to create internal list

            VerifyRefreshNotDeferred();

            if (IsUsingItemsSource)
            {
                throw new InvalidOperationException(SR.ItemsSourceInUse);
            }

            _internalView?.Clear();
            ModelParent.ClearValue(ItemsControl.HasItemsPropertyKey);
        }

        /// <summary>
        ///     Checks to see if a given item is in this collection and in the view
        /// </summary>
        /// <param name="containItem">
        ///     The item whose membership in this collection is to be checked.
        /// </param>
        /// <returns>
        ///     True if the collection contains the given item and the item passes the active filter
        /// </returns>
        public override bool Contains(object containItem)
        {
            if (!EnsureCollectionView())

View on GitHub (pinned to 81131a70a4)