{"record":{"id":"afe047f1d4c9c57a","repo":"PrismLibrary/Prism","slug":"resources-itemscontrolhasitemssourceexception-afe047","errorCode":null,"errorMessage":"Resources.ItemsControlHasItemsSourceException","messagePattern":"Resources\\.ItemsControlHasItemsSourceException","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf/Prism.Wpf/Navigation/Regions/ItemsControlRegionAdapter.cs","lineNumber":40,"sourceCode":"        /// </summary>\n        /// <param name=\"region\">The new region being used.</param>\n        /// <param name=\"regionTarget\">The object to adapt.</param>\n        protected override void Adapt(IRegion region, ItemsControl regionTarget)\n        {\n            if (region == null)\n                throw new ArgumentNullException(nameof(region));\n\n            if (regionTarget == null)\n                throw new ArgumentNullException(nameof(regionTarget));\n\n            // NOTE: In Avalonia, regionTarget.ItemsSource will not be null. Keep it rollin' baby!\n#if !AVALONIA\n            bool itemsSourceIsSet = regionTarget.ItemsSource != null;\n            itemsSourceIsSet = itemsSourceIsSet || regionTarget.HasBinding(ItemsControl.ItemsSourceProperty);\n\n            if (itemsSourceIsSet)\n            {\n                throw new InvalidOperationException(Resources.ItemsControlHasItemsSourceException);\n            }\n\n            // If control has child items, move them to the region and then bind control to region. Can't set ItemsSource if child items exist.\n            if (regionTarget.Items.Count > 0)\n            {\n                foreach (object childItem in regionTarget.Items)\n                {\n                    region.Add(childItem);\n                }\n                // Control must be empty before setting ItemsSource\n                regionTarget.Items.Clear();\n            }\n\n            regionTarget.ItemsSource = region.Views;\n#else\n            // If control has child items, move them to the region and then bind control to region. Can't set ItemsSource if child items exist.\n            if (regionTarget.ItemCount > 0)\n            {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Wpf/Prism.Wpf/Navigation/Regions/ItemsControlRegionAdapter.cs#L22-L58","documentation":"ItemsControlRegionAdapter.Adapt throws InvalidOperationException with Resources.ItemsControlHasItemsSourceException when the target ItemsControl already has an ItemsSource or an ItemsSource binding. The region must control ItemsSource to reflect the region's views, so a pre-existing source conflicts.","triggerScenarios":"Applying RegionManager.RegionName to an ItemsControl/ListBox/ComboBox that already has ItemsSource bound or assigned in XAML/code.","commonSituations":"Converting a data-bound list into a region without removing the binding; template/style-set ItemsSource that developers forget about; mixing MVVM data-binding and Prism region population on the same control.","solutions":["Remove the ItemsSource binding/assignment and populate the region instead.","Keep ItemsSource binding if you want plain data-binding — do not add RegionName to that control.","Wrap the bound control in a view and register that view with the region.","Verify no style or template sets ItemsSource (HasBinding detects it)."],"exampleFix":"// before\n<ItemsControl ItemsSource=\"{Binding Rows}\" prism:RegionManager.RegionName=\"RowsRegion\" />\n// after\n<ItemsControl prism:RegionManager.RegionName=\"RowsRegion\" />","handlingStrategy":"validation","validationCode":"if (itemsControl.ItemsSource != null || itemsControl.HasBinding(ItemsControl.ItemsSourceProperty))\n    throw new InvalidOperationException(\"ItemsControl region host must not have ItemsSource set.\");","typeGuard":"bool IsRegionCompatible(ItemsControl c) => c.ItemsSource == null && !c.HasBinding(ItemsControl.ItemsSourceProperty);","tryCatchPattern":"try\n{\n    RegionManager.SetRegionName(itemsControl, \"RowsRegion\");\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ItemsSource\"))\n{\n    logger.LogError(ex, \"ItemsControl has ItemsSource; cannot be a region host.\");\n}","preventionTips":["Choose one: ItemsSource data-binding or region population","Search XAML for RegionName alongside ItemsSource during reviews","Audit styles for ItemsSource setters"],"tags":["wpf","regions","items-control","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"}