{"record":{"id":"44501aaa2f57cddb","repo":"dotnet/wpf","slug":"sr-cannotuseitemssource","errorCode":null,"errorMessage":"SR.CannotUseItemsSource","messagePattern":"SR\\.CannotUseItemsSource","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs","lineNumber":1497,"sourceCode":"        internal DependencyObject ModelParent\n        {\n            get { return (DependencyObject)_modelParent.Target; }\n        }\n\n        internal FrameworkElement ModelParentFE\n        {\n            get { return ModelParent as FrameworkElement; }\n        }\n\n        // This puts the ItemCollection into ItemsSource mode.\n        internal void SetItemsSource(IEnumerable value, Func<object, object> GetSourceItem = null)\n        {\n            // Allow this while refresh is deferred.\n\n            // If we're switching from Normal mode, first make sure it's legal.\n            if (!IsUsingItemsSource && (_internalView != null) && (_internalView.RawCount > 0))\n            {\n                throw new InvalidOperationException(SR.CannotUseItemsSource);\n            }\n\n            _itemsSource = value;\n            _isUsingItemsSource = true;\n\n            SetCollectionView(CollectionViewSource.GetDefaultCollectionView(_itemsSource, ModelParent, GetSourceItem));\n        }\n\n        // This returns ItemCollection to direct mode.\n        internal void ClearItemsSource()\n        {\n            if (IsUsingItemsSource)\n            {\n                // return to normal mode\n                _itemsSource = null;\n                _isUsingItemsSource = false;\n\n                SetCollectionView(_internalView);   // it's ok if _internalView is null; just like uninitialized","sourceCodeStart":1479,"sourceCodeEnd":1515,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs#L1479-L1515","documentation":"An ItemCollection operates in one of two modes: using an ItemsSource or a direct 'internal' item list. Setting ItemsSource while the collection still holds directly-added items throws InvalidOperationException(SR.CannotUseItemsSource), because switching would discard existing content.","triggerScenarios":"Assigning ItemsControl.ItemsSource (which sets ItemCollection.ItemsSource) after items were added directly via Items.Add when IsUsingItemsSource is false and the internal view already has entries (RawCount > 0).","commonSituations":"Declaring <ItemsControl><ListBoxItem .../></ItemsControl> in XAML and also setting ItemsSource in code or via a binding; mixing data binding with hand-populated items in the same control.","solutions":["Clear the direct items first (Items.Clear()) before setting ItemsSource","Choose one mode: either populate via ItemsSource or via Items.Add, never both","Remove the direct children from XAML if a binding supplies the data"],"exampleFix":"// before\nlistBox.Items.Add(\"x\");\nlistBox.ItemsSource = myCollection; // throws\n// after\nlistBox.Items.Clear();\nlistBox.ItemsSource = myCollection;","handlingStrategy":"validation","validationCode":"if (items.Count > 0) items.Clear();\nitemsControl.ItemsSource = myCollection;","typeGuard":"bool safeToSetSource = items == null || items.Count == 0;","tryCatchPattern":"try { itemsControl.ItemsSource = src; } catch (InvalidOperationException ex) { itemsControl.Items.Clear(); itemsControl.ItemsSource = src; }","preventionTips":["Never mix Items.Add with ItemsSource","Clear direct items before switching modes","Set ItemsSource in one place (e.g. XAML binding) only"],"tags":["wpf","itemcollection","itemssource"],"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"}