{"record":{"id":"8805bc077b3e7e68","repo":"PrismLibrary/Prism","slug":"resources-regionbehaviorregioncannotbesetafterattach","errorCode":null,"errorMessage":"Resources.RegionBehaviorRegionCannotBeSetAfterAttach","messagePattern":"Resources\\.RegionBehaviorRegionCannotBeSetAfterAttach","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Navigation/Regions/RegionBehavior.cs","lineNumber":26,"sourceCode":"    /// </summary>\n    public abstract class RegionBehavior : IRegionBehavior\n    {\n        private IRegion region;\n\n        /// <summary>\n        /// Behavior's attached region.\n        /// </summary>\n        public IRegion Region\n        {\n            get\n            {\n                return region;\n            }\n            set\n            {\n                if (this.IsAttached)\n                {\n                    throw new InvalidOperationException(Resources.RegionBehaviorRegionCannotBeSetAfterAttach);\n                }\n\n                this.region = value;\n            }\n        }\n\n        /// <summary>\n        /// Returns <see langword=\"true\"/> if the behavior is attached to a region, <see langword=\"false\"/> otherwise.\n        /// </summary>\n        public bool IsAttached { get; private set; }\n\n        /// <summary>\n        /// Attaches the behavior to the region.\n        /// </summary>\n        public void Attach()\n        {\n            if (this.region == null)\n            {","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Navigation/Regions/RegionBehavior.cs#L8-L44","documentation":"RegionBehavior.Region throws InvalidOperationException if you set the Region property after Attach() was called. A behavior is designed to be attached to exactly one region; re-pointing it afterward would corrupt region state.","triggerScenarios":"Assigning behavior.Region = someRegion after behavior.Attach() has already run — commonly when a behavior instance is shared/reused across two regions.","commonSituations":"Reusing a singleton IRegionBehavior for multiple regions; re-registering behaviors in a custom region adapter; region re-creation reusing old behavior instances.","solutions":["Create a new behavior instance per region instead of reusing one","Only set Region before calling Attach()","If using RegionBehaviorCollection.Add, let it set Region and call Attach for you"],"exampleFix":"// before\nsharedBehavior.Region = newRegion; // already attached\n// after\nvar behavior = new MyBehavior { Region = newRegion };\nbehavior.Attach();","handlingStrategy":"validation","validationCode":"if (behavior.IsAttached) throw new InvalidOperationException(\"Behavior already attached; create a new instance.\");","typeGuard":"bool Reusable(IRegionBehavior b) => !b.IsAttached;","tryCatchPattern":"try { behavior.Region = r; } catch (InvalidOperationException) { behavior = NewBehavior(r); }","preventionTips":["Never share behavior instances between regions","Set Region only before Attach","Let RegionBehaviorCollection.Add handle wiring"],"tags":["prism","regions","invalid-operation","state"],"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"}