{"record":{"id":"bd2f49da64e11775","repo":"dotnet/wpf","slug":"sr-collectionviewtypeisinitonly","errorCode":null,"errorMessage":"SR.CollectionViewTypeIsInitOnly","messagePattern":"SR\\.CollectionViewTypeIsInitOnly","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionViewSource.cs","lineNumber":184,"sourceCode":"        /// </summary>\n        /// <remarks>\n        ///     This property may only be set during initialization.\n        /// </remarks>\n        public Type CollectionViewType\n        {\n            get { return (Type) GetValue(CollectionViewTypeProperty); }\n            set { SetValue(CollectionViewTypeProperty, value); }\n        }\n\n        private static void OnCollectionViewTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)\n        {\n            CollectionViewSource ctrl = (CollectionViewSource) d;\n\n            Type oldCollectionViewType = (Type) e.OldValue;\n            Type newCollectionViewType = (Type) e.NewValue;\n\n            if (!ctrl._isInitializing)\n                throw new InvalidOperationException(SR.CollectionViewTypeIsInitOnly);\n\n            ctrl.OnCollectionViewTypeChanged(oldCollectionViewType, newCollectionViewType);\n            ctrl.EnsureView();\n        }\n\n        /// <summary>\n        ///     This method is invoked when the CollectionViewType property changes.\n        /// </summary>\n        /// <param name=\"oldCollectionViewType\">The old value of the CollectionViewType property.</param>\n        /// <param name=\"newCollectionViewType\">The new value of the CollectionViewType property.</param>\n        protected virtual void OnCollectionViewTypeChanged(Type oldCollectionViewType, Type newCollectionViewType)\n        {\n        }\n\n        private static bool IsCollectionViewTypeValid(object o)\n        {\n            Type type = (Type)o;\n","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionViewSource.cs#L166-L202","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set CollectionViewType only in XAML at object creation or inside BeginInit()/EndInit() before EndInit","Remove runtime reassignment of CollectionViewType; create a new CollectionViewSource instead","If the view type must vary, construct the appropriate view source per scenario rather than mutating one instance"],"exampleFix":"// before\nvar cvs = new CollectionViewSource { Source = items };\ncvs.CollectionViewType = typeof(ListCollectionView); // throws\n// after\nvar cvs = new CollectionViewSource();\ncvs.BeginInit();\ncvs.CollectionViewType = typeof(ListCollectionView);\ncvs.Source = items;\ncvs.EndInit();","handlingStrategy":"validation","validationCode":"// set CollectionViewType only between BeginInit/EndInit\nbool canSetCollectionViewType(CollectionViewSource cvs) => isInsideInitWindow; // track via BeginInit/EndInit overrides","typeGuard":"bool IsInitOnlySafe(Action mutate) { cvs.BeginInit(); try { mutate(); } finally { cvs.EndInit(); } return true; }","tryCatchPattern":"try { cvs.CollectionViewType = t; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"CollectionViewTypeIsInitOnly\")) { /* recreate source inside init window */ }","preventionTips":["Configure CollectionViewType in XAML, not runtime code","Wrap programmatic configuration in BeginInit/EndInit","Create a new CollectionViewSource when the view type must change"],"tags":["wpf","data-binding","collectionviewsource","invalid-state-transition"],"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"}