dotnet/wpf · error · NotSupportedException

NotSupportedException

Error message

NotSupportedException

What it means

SortDescriptionCollection is a read-only collection: ClearItems (called by Collection<T>.Clear) always throws NotSupportedException. The collection only supports adding via its public API and removal through ReplaceItem/clearing semantics defined by the view, not direct Clear().

Solutions

  1. Remove items individually with RemoveAt/Remove (if the view allows) or replace the whole collection reference.
  2. Rebuild sorting by assigning a fresh SortDescriptionCollection or by replacing items in-place via the indexer.
  3. Wrap the Clear call in a loop replacing entries, or clear via ICollectionView by setting a new SortDescriptions set.

Example fix

// before
view.SortDescriptions.Clear(); // throws
// after
while (view.SortDescriptions.Count > 0) view.SortDescriptions.RemoveAt(view.SortDescriptions.Count - 1);
Defensive patterns

Strategy: validation

Validate before calling

if (view.SortDescriptions is SortDescriptionCollection && view.SortDescriptions.Count > 0) { /* clear via RemoveAt loop or rebuild */ }

Try / catch

try { view.SortDescriptions.Clear(); } catch (NotSupportedException) { /* clear via RemoveAt loop */ }

Prevention

When it happens

Trigger: Calling myView.SortDescriptions.Clear() or otherwise triggering ICollection<T>.Clear on a SortDescriptionCollection instance.

Common situations: Resetting sort criteria before applying new ones in WPF collection-view code.

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/2a82386ff2eaf6ef. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/SortDescriptionCollection.cs:143

        /// Immutable, read-only SortDescriptionCollection
        /// </summary>
        private class EmptySortDescriptionCollection : SortDescriptionCollection, IList
        {
            //------------------------------------------------------
            //
            //  Protected Methods
            //
            //------------------------------------------------------

            #region Protected Methods

            /// <summary>
            /// called by base class Collection&lt;T&gt; when the list is being cleared;
            /// raises a CollectionChanged event to any listeners
            /// </summary>
            protected override void ClearItems()
            {
                throw new NotSupportedException();
            }

            /// <summary>
            /// called by base class Collection&lt;T&gt; when an item is removed from list;
            /// raises a CollectionChanged event to any listeners
            /// </summary>
            protected override void RemoveItem(int index)
            {
                throw new NotSupportedException();
            }

            /// <summary>
            /// called by base class Collection&lt;T&gt; when an item is added to list;
            /// raises a CollectionChanged event to any listeners
            /// </summary>
            protected override void InsertItem(int index, SortDescription item)
            {
                throw new NotSupportedException();

View on GitHub (pinned to 81131a70a4)