dotnet/wpf · error · InvalidOperationException

SR.CollectionViewTypeIsInitOnly

Error message

SR.CollectionViewTypeIsInitOnly

What it means

CollectionViewSource.CollectionViewType is an init-only property: it may only be set during initialization (e.g. in XAML object element usage or within the ISupportInitialize BeginInit/EndInit window). Setting it after the CollectionViewSource has been initialized throws InvalidOperationException with CollectionViewTypeIsInitOnly.

Solutions

  1. Set CollectionViewType only in XAML at object creation or inside BeginInit()/EndInit() before EndInit
  2. Remove runtime reassignment of CollectionViewType; create a new CollectionViewSource instead
  3. If the view type must vary, construct the appropriate view source per scenario rather than mutating one instance

Example fix

// before
var cvs = new CollectionViewSource { Source = items };
cvs.CollectionViewType = typeof(ListCollectionView); // throws
// after
var cvs = new CollectionViewSource();
cvs.BeginInit();
cvs.CollectionViewType = typeof(ListCollectionView);
cvs.Source = items;
cvs.EndInit();
Defensive patterns

Strategy: validation

Validate before calling

// set CollectionViewType only between BeginInit/EndInit
bool canSetCollectionViewType(CollectionViewSource cvs) => isInsideInitWindow; // track via BeginInit/EndInit overrides

Type guard

bool IsInitOnlySafe(Action mutate) { cvs.BeginInit(); try { mutate(); } finally { cvs.EndInit(); } return true; }

Try / catch

try { cvs.CollectionViewType = t; } catch (InvalidOperationException ex) when (ex.Message.Contains("CollectionViewTypeIsInitOnly")) { /* recreate source inside init window */ }

Prevention

When it happens

Trigger: Assigning cvSource.CollectionViewType = typeof(ListCollectionView) in imperative code after construction and after initialization completes; setting it in a style setter or animation; changing it after EndInit was called.

Common situations: Developers converting XAML-based CollectionViewSource usage to code-behind and setting the property at runtime; attempting to switch view type dynamically when the data source changes.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionViewSource.cs:184

        /// </summary>
        /// <remarks>
        ///     This property may only be set during initialization.
        /// </remarks>
        public Type CollectionViewType
        {
            get { return (Type) GetValue(CollectionViewTypeProperty); }
            set { SetValue(CollectionViewTypeProperty, value); }
        }

        private static void OnCollectionViewTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CollectionViewSource ctrl = (CollectionViewSource) d;

            Type oldCollectionViewType = (Type) e.OldValue;
            Type newCollectionViewType = (Type) e.NewValue;

            if (!ctrl._isInitializing)
                throw new InvalidOperationException(SR.CollectionViewTypeIsInitOnly);

            ctrl.OnCollectionViewTypeChanged(oldCollectionViewType, newCollectionViewType);
            ctrl.EnsureView();
        }

        /// <summary>
        ///     This method is invoked when the CollectionViewType property changes.
        /// </summary>
        /// <param name="oldCollectionViewType">The old value of the CollectionViewType property.</param>
        /// <param name="newCollectionViewType">The new value of the CollectionViewType property.</param>
        protected virtual void OnCollectionViewTypeChanged(Type oldCollectionViewType, Type newCollectionViewType)
        {
        }

        private static bool IsCollectionViewTypeValid(object o)
        {
            Type type = (Type)o;

View on GitHub (pinned to 81131a70a4)