{"record":{"id":"62967b74f7c1f1fb","repo":"PrismLibrary/Prism","slug":"resources-hostcontrolcannotbesetafterattach","errorCode":null,"errorMessage":"Resources.HostControlCannotBeSetAfterAttach","messagePattern":"Resources\\.HostControlCannotBeSetAfterAttach","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf/Prism.Wpf/Navigation/Regions/Behaviors/RegionManagerRegistrationBehavior.cs","lineNumber":55,"sourceCode":"        public IRegionManagerAccessor RegionManagerAccessor { get; set; }\n\n        /// <summary>\n        /// Gets or sets the <see cref=\"DependencyObject\"/> that the <see cref=\"IRegion\"/> is attached to.\n        /// </summary>\n        /// <value>A <see cref=\"DependencyObject\"/> that the <see cref=\"IRegion\"/> is attached to.\n        /// This is usually a <see cref=\"FrameworkElement\"/> that is part of the tree.</value>\n        /// <exception cref=\"InvalidOperationException\">When this member is set after the <see cref=\"IRegionBehavior.Attach\"/> method has being called.</exception>\n        public DependencyObject HostControl\n        {\n            get\n            {\n                return hostControl;\n            }\n            set\n            {\n                if (IsAttached)\n                {\n                    throw new InvalidOperationException(Resources.HostControlCannotBeSetAfterAttach);\n                }\n\n                hostControl = value;\n            }\n        }\n\n        /// <summary>\n        /// When the <see cref=\"IRegion\"/> has a name assigned, the behavior will start monitoring the ancestor controls in the element tree\n        /// to look for an <see cref=\"IRegionManager\"/> where to register the region in.\n        /// </summary>\n        protected override void OnAttach()\n        {\n            if (string.IsNullOrEmpty(Region.Name))\n            {\n                Region.PropertyChanged += Region_PropertyChanged;\n            }\n            else\n            {","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Wpf/Prism.Wpf/Navigation/Regions/Behaviors/RegionManagerRegistrationBehavior.cs#L37-L73","documentation":"RegionManagerRegistrationBehavior.HostControl setter throws InvalidOperationException with Resources.HostControlCannotBeSetAfterAttach when the behavior is already attached (IsAttached == true). The host control is the target the behavior monitors, so changing it after attach would leave the region registration in an inconsistent state.","triggerScenarios":"Setting behavior.HostControl after behavior.Attach() has run — e.g., resolving the behavior from a region's Behaviors collection and reassigning its HostControl, or data-binding/two-way assignments that fire after attach.","commonSituations":"Custom region setup code that reuses behavior instances across controls; XAML templates recreating the visual host while reusing the behavior; test code manipulating behaviors directly after attach.","solutions":["Set HostControl before calling Attach(); create a new behavior instance if the host must change.","Reorder initialization code so HostControl is assigned first and Attach() is last.","Detach the old behavior (IsAttached false) and build a fresh one for a new host control.","Avoid binding HostControl to a property that changes after the region attaches."],"exampleFix":"// before\nbehavior.Attach();\nbehavior.HostControl = newHost; // throws\n// after\nbehavior.HostControl = newHost;\nbehavior.Attach();","handlingStrategy":"validation","validationCode":"if (behavior.IsAttached)\n    throw new InvalidOperationException(\"HostControl must be set before Attach().\");","typeGuard":"bool CanSetHostControl(RegionManagerRegistrationBehavior b) => !b.IsAttached;","tryCatchPattern":"try\n{\n    behavior.HostControl = host;\n}\ncatch (InvalidOperationException)\n{\n    // behavior already attached — create a new behavior for this host\n    behavior = new RegionManagerRegistrationBehavior { RegionManager = rm, RegionName = name, HostControl = host };\n    behavior.Attach();\n}","preventionTips":["Always set HostControl before Attach()","Never reuse behavior instances across host controls","Review custom region bootstrap ordering"],"tags":["wpf","regions","behaviors","invalid-operation"],"backgroundTag":"invalid-state-transition","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"}