{"record":{"id":"201502d7166b59b4","repo":"dotnet/wpf","slug":"sr-objectdataproviderparametercollectionisnotinuse","errorCode":null,"errorMessage":"SR.ObjectDataProviderParameterCollectionIsNotInUse","messagePattern":"SR\\.ObjectDataProviderParameterCollectionIsNotInUse","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/ParameterCollection.cs","lineNumber":170,"sourceCode":"        {\n            base.ClearItems();\n        }\n\n        #endregion Internal Methods\n\n        //------------------------------------------------------\n        //\n        //  Private Methods\n        //\n        //------------------------------------------------------\n\n        #region Private Methods\n\n        private void CheckReadOnly()\n        {\n            if (this.IsReadOnly)\n            {\n                throw new InvalidOperationException(SR.ObjectDataProviderParameterCollectionIsNotInUse);\n            }\n        }\n\n        /// <summary>\n        /// notify ObjectDataProvider that the parameters have changed\n        /// </summary>\n        private void OnCollectionChanged()\n        {\n            _parametersChanged(this);\n        }\n\n        #endregion Private Methods\n\n        //------------------------------------------------------\n        //\n        //  Private Fields\n        //\n        //------------------------------------------------------","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/ParameterCollection.cs#L152-L188","documentation":"ParameterCollection (backing ObjectDataProvider's ConstructorParameters/ObjectInstance/MethodParameters) is read-only once it is in active use by an ObjectDataProvider; CheckReadOnly throws InvalidOperationException with SR.ObjectDataProviderParameterCollectionIsNotInUse on any mutation. WPF marks the collection in use when the provider starts constructing/invoking, so late mutation would corrupt provider state.","triggerScenarios":"Adding/inserting/removing/clearing/setting items in a ParameterCollection that ObjectDataProvider has already flagged in-use — e.g. mutating MethodParameters during the object's construction callback, from a data-binding converter, or after Refresh without detaching.","commonSituations":"XAML data binding where code-behind or a converter modifies ObjectDataProvider.MethodParameters while the provider is mid-creation; mutating parameters inside the target object's constructor triggered by the provider.","solutions":["Wait until the provider is idle: only mutate MethodParameters/ConstructorParameters outside active refresh cycles (e.g. before the first Refresh, or after the binding completes).","Detach first: clear the binding (set the Binding's source differently) or reassign a fresh ObjectDataProvider with new parameters.","Batch parameter changes and apply them on the dispatcher at a safe point (Dispatcher.BeginInvoke at Background priority), then call provider.Refresh().","Wrap mutations in try-catch InvalidOperationException to detect the in-use state and defer the change."],"exampleFix":"// before\nprovider.MethodParameters.Clear(); // InvalidOperationException: collection is not in use\n\n// after\nDispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>\n{\n    provider.MethodParameters.Clear();\n    provider.MethodParameters.Add(newValue);\n    provider.Refresh();\n}));","handlingStrategy":"try-catch","validationCode":"// mutate parameters only before first use; after Refresh, defer mutations\nbool safe = !isProviderActive; // track provider activity around Refresh/construction","typeGuard":null,"tryCatchPattern":"try { provider.MethodParameters.Add(value); provider.Refresh(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"use\"))\n{\n    Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>\n    { provider.MethodParameters.Add(value); provider.Refresh(); }));\n}","preventionTips":["Never mutate MethodParameters/ConstructorParameters from inside the created object's constructor.","Change parameters only while the ObjectDataProvider is idle; call Refresh afterwards.","Replace an in-use provider with a new instance instead of mutating its collection.","Defer parameter changes to the dispatcher queue at background priority."],"tags":["wpf","objectdataprovider","readonly-collection","invalidoperationexception"],"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"}