{"record":{"id":"773abb0614851d20","repo":"dotnet/wpf","slug":"sr-itemssourceinuse","errorCode":null,"errorMessage":"SR.ItemsSourceInUse","messagePattern":"SR\\.ItemsSourceInUse","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs","lineNumber":246,"sourceCode":"            return index;\n        }\n\n        /// <summary>\n        ///     Clears the collection.  Releases the references on all items\n        /// currently in the collection.\n        /// </summary>\n        /// <exception cref=\"InvalidOperationException\">\n        /// the ItemCollection is read-only because it is in ItemsSource mode\n        /// </exception>\n        public void Clear()\n        {\n            // Not using CheckIsUsingInnerView() because we don't want to create internal list\n\n            VerifyRefreshNotDeferred();\n\n            if (IsUsingItemsSource)\n            {\n                throw new InvalidOperationException(SR.ItemsSourceInUse);\n            }\n\n            _internalView?.Clear();\n            ModelParent.ClearValue(ItemsControl.HasItemsPropertyKey);\n        }\n\n        /// <summary>\n        ///     Checks to see if a given item is in this collection and in the view\n        /// </summary>\n        /// <param name=\"containItem\">\n        ///     The item whose membership in this collection is to be checked.\n        /// </param>\n        /// <returns>\n        ///     True if the collection contains the given item and the item passes the active filter\n        /// </returns>\n        public override bool Contains(object containItem)\n        {\n            if (!EnsureCollectionView())","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs#L228-L264","documentation":"ItemCollection.Clear throws InvalidOperationException (SR.ItemsSourceInUse) when the collection is backed by an ItemsSource (IsUsingItemsSource). In that mode the ItemsControl delegates storage to the source collection, so direct mutation of ItemCollection is not allowed. The comment notes it deliberately avoids creating the internal view and also verifies refresh is not deferred.","triggerScenarios":"Calling items.Clear() (or other direct mutations) on an ItemsControl's Items collection while ItemsSource is set. Any manipulation of Items (Add/Remove/Clear) with ItemsSource assigned throws this family of errors.","commonSituations":"Setting ItemsSource in XAML/data binding but also trying to clear/seed Items in code-behind; switching between manual items and bound mode without clearing ItemsSource first.","solutions":["Clear the underlying source collection instead of Items (e.g. ObservableCollection.Clear)","Set ItemsSource = null before mutating Items directly","Choose one mode: either bind via ItemsSource or populate Items manually, not both"],"exampleFix":"// before\nlistBox.Items.Clear(); // ItemsSource is set\n// after\nvar oc = listBox.ItemsSource as ObservableCollection<Item>;\noc?.Clear();","handlingStrategy":"validation","validationCode":"bool canClearDirectly = !(items as ItemCollection)?.IsUsingItemsSource ?? true; // mutate source collection if bound","typeGuard":"bool IsBound(ItemCollection c) => c.SourceCollection != c; // SourceCollection differs when ItemsSource is in use","tryCatchPattern":"try { itemsControl.Items.Clear(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"ItemsSource\")) { (itemsControl.ItemsSource as System.Collections.IList)?.Clear(); }","preventionTips":["Never mutate Items when ItemsSource is assigned; mutate the source collection","Pick one population mode per ItemsControl (binding or manual)","Null out ItemsSource before direct Items manipulation in transitional scenarios"],"tags":["wpf","itemcollection","itemssource","invalidoperation"],"backgroundTag":"unsupported-operation","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"}