{"record":{"id":"965ceac7216eaecf","repo":"dotnet/wpf","slug":"sr-cannotchangeaftersealed-sortdescription","errorCode":null,"errorMessage":"SR.CannotChangeAfterSealed","messagePattern":"SR\\.CannotChangeAfterSealed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/SortDescription.cs","lineNumber":55,"sourceCode":"\n        //------------------------------------------------------\n        //\n        //  Public Properties\n        //\n        //------------------------------------------------------\n\n        #region Public Properties\n\n        /// <summary>\n        /// Property name to sort by.\n        /// </summary>\n        public string PropertyName\n        {\n            get { return _propertyName; }\n            set\n            {\n                if (_sealed)\n                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"SortDescription\"));\n\n                _propertyName = value;\n            }\n        }\n\n        /// <summary>\n        /// Sort direction.\n        /// </summary>\n        public ListSortDirection Direction\n        {\n            get { return _direction; }\n            set\n            {\n                if (_sealed)\n                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"SortDescription\"));\n\n                if (value < ListSortDirection.Ascending || value > ListSortDirection.Descending)\n                    throw new InvalidEnumArgumentException(\"value\", (int) value, typeof(ListSortDirection));","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/SortDescription.cs#L37-L73","documentation":"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.","triggerScenarios":"Modifying sortDescription.PropertyName (or Direction setter) after the description was added to a view's SortDescriptions collection, which seals it.","commonSituations":"Re-sorting code that mutates an existing SortDescription instance instead of replacing it; cached SortDescription objects reused across views.","solutions":["Create a new SortDescription with the desired PropertyName and replace the sealed one via view.SortDescriptions[i] = newDesc (or Clear+Add)","Build fresh SortDescriptions each time sorting criteria change","Keep your own mutable description template and instantiate a SortDescription only when applying it"],"exampleFix":"// before\nview.SortDescriptions[0].PropertyName = \"Age\"; // InvalidOperationException\n// after\nvar old = view.SortDescriptions[0];\nview.SortDescriptions[0] = new SortDescription(\"Age\", old.Direction);","handlingStrategy":"validation","validationCode":"if (view.SortDescriptions.IsSealed) throw new InvalidOperationException(\"SortDescriptions cannot change now; create new SortDescription instances\");","typeGuard":"static bool CanEdit(SortDescription sd) => !sd.IsSealed;","tryCatchPattern":"try { sd.PropertyName = newName; } catch (InvalidOperationException) { view.SortDescriptions[i] = new SortDescription(newName, sd.Direction); }","preventionTips":["Treat SortDescription as immutable once added to a view","Replace descriptions instead of mutating them","Check the IsSealed property before mutating"],"tags":["wpf","sorting","invalid-operation","sealed"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}