dotnet/wpf · error · InvalidOperationException

SR.CannotChangeAfterSealed

Error message

SR.CannotChangeAfterSealed

What it means

Once a SortDescription is sealed (added to a SortDescriptionCollection in use by a view), its PropertyName setter throws InvalidOperationException with SR.CannotChangeAfterSealed. Sealing prevents changing the sort key while a CollectionView is actively sorting by it.

Solutions

  1. Create a new SortDescription with the desired PropertyName and replace the sealed one via view.SortDescriptions[i] = newDesc (or Clear+Add)
  2. Build fresh SortDescriptions each time sorting criteria change
  3. Keep your own mutable description template and instantiate a SortDescription only when applying it

Example fix

// before
view.SortDescriptions[0].PropertyName = "Age"; // InvalidOperationException
// after
var old = view.SortDescriptions[0];
view.SortDescriptions[0] = new SortDescription("Age", old.Direction);
Defensive patterns

Strategy: validation

Validate before calling

if (view.SortDescriptions.IsSealed) throw new InvalidOperationException("SortDescriptions cannot change now; create new SortDescription instances");

Type guard

static bool CanEdit(SortDescription sd) => !sd.IsSealed;

Try / catch

try { sd.PropertyName = newName; } catch (InvalidOperationException) { view.SortDescriptions[i] = new SortDescription(newName, sd.Direction); }

Prevention

When it happens

Trigger: Modifying sortDescription.PropertyName (or Direction setter) after the description was added to a view's SortDescriptions collection, which seals it.

Common situations: Re-sorting code that mutates an existing SortDescription instance instead of replacing it; cached SortDescription objects reused across views.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/SortDescription.cs:55

        //------------------------------------------------------
        //
        //  Public Properties
        //
        //------------------------------------------------------

        #region Public Properties

        /// <summary>
        /// Property name to sort by.
        /// </summary>
        public string PropertyName
        {
            get { return _propertyName; }
            set
            {
                if (_sealed)
                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "SortDescription"));

                _propertyName = value;
            }
        }

        /// <summary>
        /// Sort direction.
        /// </summary>
        public ListSortDirection Direction
        {
            get { return _direction; }
            set
            {
                if (_sealed)
                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "SortDescription"));

                if (value < ListSortDirection.Ascending || value > ListSortDirection.Descending)
                    throw new InvalidEnumArgumentException("value", (int) value, typeof(ListSortDirection));

View on GitHub (pinned to 81131a70a4)