{"record":{"id":"9eaf49d2835fcb24","repo":"PrismLibrary/Prism","slug":"resources-hostcontrolcannotbesetafterattach-9eaf49","errorCode":null,"errorMessage":"Resources.HostControlCannotBeSetAfterAttach","messagePattern":"Resources\\.HostControlCannotBeSetAfterAttach","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf/Prism.Wpf/Navigation/Regions/Behaviors/SyncRegionContextWithHostBehavior.cs","lineNumber":48,"sourceCode":"\n        /// <summary>\n        /// Gets or sets the <see cref=\"DependencyObject\"/> that the <see cref=\"IRegion\"/> is attached to.\n        /// </summary>\n        /// <value>\n        /// 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.\n        /// </value>\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        /// Override this method to perform the logic after the behavior has been attached.\n        /// </summary>\n        protected override void OnAttach()\n        {\n            if (HostControl != null)\n            {\n                // Sync values initially.\n                SynchronizeRegionContext();\n\n                // Now register for events to keep them in sync\n                HostControlRegionContext.PropertyChanged += RegionContextObservableObject_PropertyChanged;","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Wpf/Prism.Wpf/Navigation/Regions/Behaviors/SyncRegionContextWithHostBehavior.cs#L30-L66","documentation":"SyncRegionContextWithHostBehavior.HostControl setter throws InvalidOperationException with Resources.HostControlCannotBeSetAfterAttach when the behavior has already been attached. Like the other region behaviors, the host control must be fixed before attach because the behavior wires context synchronization to it at attach time.","triggerScenarios":"Assigning behavior.HostControl after behavior.Attach() — via custom region bootstrap code, resolved behavior instances, or template re-application that triggers the setter post-attach.","commonSituations":"Programmatic region construction where property assignment order is wrong; refactoring that moved Attach earlier; unit tests that mutate behaviors after attach.","solutions":["Assign HostControl before calling Attach().","Create a new SyncRegionContextWithHostBehavior instance for a different host instead of reassigning.","Audit any custom region/behavior setup code for property-assignment order.","Do not data-bind HostControl to values that change after region attach."],"exampleFix":"// before\nsyncBehavior.Attach();\nsyncBehavior.HostControl = host; // throws\n// after\nsyncBehavior.HostControl = host;\nsyncBehavior.Attach();","handlingStrategy":"validation","validationCode":"if (syncBehavior.IsAttached)\n    throw new InvalidOperationException(\"Set HostControl before Attach().\");","typeGuard":"bool CanAssignHost(SyncRegionContextWithHostBehavior b) => !b.IsAttached;","tryCatchPattern":"try\n{\n    syncBehavior.HostControl = host;\n}\ncatch (InvalidOperationException)\n{\n    syncBehavior = new SyncRegionContextWithHostBehavior { HostControl = host };\n    syncBehavior.Attach();\n}","preventionTips":["Configure behavior properties before Attach()","Instantiate a fresh behavior for each host control","Avoid data-binding HostControl to mutable values"],"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"}