dotnet/wpf · error · InvalidOperationException

SR.Format(SR.ImplementOtherMembersWithSort, "Refresh()")

Error message

SR.Format(SR.ImplementOtherMembersWithSort, "Refresh()")

What it means

The base CollectionView.RefreshOverride throws InvalidOperationException if SortDescriptions is non-empty. The base implementation does not perform sorting; a derived view must implement sorting (and allow SortDescriptions) itself, so calling Refresh on the base view while sort descriptions exist is an error rather than a silent no-op.

Solutions

  1. Use ListCollectionView (obtained via CollectionViewSource.GetDefaultView over an IList) which implements sorting
  2. Clear SortDescriptions before refreshing a base CollectionView
  3. Implement sorting in a custom view by overriding RefreshOverride to honor SortDescriptions
  4. Sort the underlying collection directly instead of using view SortDescriptions

Example fix

// before
var view = (CollectionView)CollectionViewSource.GetDefaultView(enumerable); // not IList
view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); // throws on Refresh
// after
var view = (ListCollectionView)CollectionViewSource.GetDefaultView(new ObservableCollection<T>(list));
view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
Defensive patterns

Strategy: validation

Validate before calling

if (view is CollectionView cv && cv.SortDescriptions.Count > 0 && view.GetType() == typeof(CollectionView))
    cv.SortDescriptions.Clear(); // or switch to ListCollectionView

Type guard

bool sortCapable = view is ListCollectionView || view.GetType() != typeof(CollectionView);

Try / catch

try { view.Refresh(); }
catch (InvalidOperationException) { view.SortDescriptions.Clear(); view.Refresh(); }

Prevention

When it happens

Trigger: Adding one or more SortDescriptions to a plain CollectionView (not a ListCollectionView or other sorted-capable derived view) and then triggering Refresh — e.g. by changing Filter, calling Refresh(), or the view being refreshed internally.

Common situations: Sorting an ItemsControl bound to a plain CollectionView over IEnumerable instead of ListCollectionView; custom CollectionView subclass overriding RefreshOverride without handling SortDescriptions.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs:837

        #endregion Public Events


        //------------------------------------------------------
        //
        //  Protected Methods
        //
        //------------------------------------------------------

        #region Protected Methods

        /// <summary>
        /// Re-create the view, using any <seealso cref="SortDescriptions"/> and/or <seealso cref="Filter"/>.
        /// </summary>
        protected virtual void RefreshOverride()
        {
            if (SortDescriptions.Count > 0)
                throw new InvalidOperationException(SR.Format(SR.ImplementOtherMembersWithSort, "Refresh()"));

            object oldCurrentItem = _currentItem;
            bool oldIsCurrentAfterLast = CheckFlag(CollectionViewFlags.IsCurrentAfterLast);
            bool oldIsCurrentBeforeFirst = CheckFlag(CollectionViewFlags.IsCurrentBeforeFirst);
            int oldCurrentPosition = _currentPosition;

            // force currency off the collection (gives user a chance to save dirty information)
            OnCurrentChanging();

            InvalidateEnumerableWrapper();

            if (IsEmpty || oldIsCurrentBeforeFirst)
            {
                _MoveCurrentToPosition(-1);
            }
            else if (oldIsCurrentAfterLast)
            {
                _MoveCurrentToPosition(Count);

View on GitHub (pinned to 81131a70a4)