{"record":{"id":"72af5895451347fd","repo":"PrismLibrary/Prism","slug":"resources-itemscontrolhasitemssourceexception","errorCode":null,"errorMessage":"Resources.ItemsControlHasItemsSourceException","messagePattern":"Resources\\.ItemsControlHasItemsSourceException","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf/Prism.Wpf/Navigation/Regions/Behaviors/SelectorItemsSourceSyncBehavior.cs","lineNumber":55,"sourceCode":"            }\n\n            set\n            {\n                _hostControl = value as Selector;\n            }\n        }\n\n        /// <summary>\n        /// Starts to monitor the <see cref=\"IRegion\"/> to keep it in sync with the items of the <see cref=\"HostControl\"/>.\n        /// </summary>\n        protected override void OnAttach()\n        {\n            bool itemsSourceIsSet = _hostControl.ItemsSource != null;\n            itemsSourceIsSet = itemsSourceIsSet || _hostControl.HasBinding(ItemsControl.ItemsSourceProperty);\n\n            if (itemsSourceIsSet)\n            {\n                throw new InvalidOperationException(Resources.ItemsControlHasItemsSourceException);\n            }\n\n            SynchronizeItems();\n\n            _hostControl.SelectionChanged += HostControlSelectionChanged;\n            Region.ActiveViews.CollectionChanged += ActiveViews_CollectionChanged;\n            Region.Views.CollectionChanged += Views_CollectionChanged;\n        }\n\n        private void Views_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)\n        {\n            if (e.Action == NotifyCollectionChangedAction.Add)\n            {\n                int startIndex = e.NewStartingIndex;\n                foreach (object newItem in e.NewItems)\n                {\n                    _hostControl.Items.Insert(startIndex++, newItem);\n                }","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Wpf/Prism.Wpf/Navigation/Regions/Behaviors/SelectorItemsSourceSyncBehavior.cs#L37-L73","documentation":"SelectorItemsSourceSyncBehavior.OnAttach validates that the region's host control (an ItemsControl/Selector) does not already have an ItemsSource or an ItemsSource binding, throwing InvalidOperationException with Resources.ItemsControlHasItemsSourceException otherwise. The behavior must own ItemsSource to synchronize it with the region's views.","triggerScenarios":"Attaching a region to a control (via RegionManager.RegionName) that already has ItemsSource set in XAML or code, or that has a binding on ItemsControl.ItemsSourceProperty.","commonSituations":"Developers binding a ListBox's ItemsSource in XAML and then adding RegionName to the same control; MVVM code assigning ItemsSource in the view-model-connected code-behind; converting an existing data-bound list into a region.","solutions":["Remove the ItemsSource assignment/binding from the host control and let the region populate it (add views via region.Add or RegisterViewWithRegion).","If data-binding is required, use the control directly without RegionName, or host the region inside a separate container control.","Move the data to a view-model and register a view that renders it inside the region.","Check style/implicit bindings — HasBinding also catches ItemsSource set through styles/templates."],"exampleFix":"// before\n<ListBox ItemsSource=\"{Binding Items}\" prism:RegionManager.RegionName=\"ListRegion\" />\n// after\n<ListBox prism:RegionManager.RegionName=\"ListRegion\" />","handlingStrategy":"validation","validationCode":"if (hostControl.ItemsSource != null || hostControl.HasBinding(ItemsControl.ItemsSourceProperty))\n    throw new InvalidOperationException(\"Remove ItemsSource before attaching a region to this control.\");","typeGuard":"bool IsRegionCompatible(ItemsControl c) => c.ItemsSource == null && !c.HasBinding(ItemsControl.ItemsSourceProperty);","tryCatchPattern":"try\n{\n    RegionManager.SetRegionName(listBox, \"ListRegion\");\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ItemsSource\"))\n{\n    logger.LogError(ex, \"Control has ItemsSource set; region cannot take ownership.\");\n}","preventionTips":["Never set ItemsSource on controls marked with RegionName","Check styles/templates for implicit ItemsSource bindings","Use regions OR direct data-binding on a control, not both"],"tags":["wpf","regions","behaviors","items-source","conflict"],"backgroundTag":"mutually-exclusive-options","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}