PrismLibrary/Prism · error · InvalidOperationException

Resources.RegionBehaviorRegionCannotBeSetAfterAttach

Error message

Resources.RegionBehaviorRegionCannotBeSetAfterAttach

What it means

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.

Solutions

  1. Create a new behavior instance per region instead of reusing one
  2. Only set Region before calling Attach()
  3. If using RegionBehaviorCollection.Add, let it set Region and call Attach for you

Example fix

// before
sharedBehavior.Region = newRegion; // already attached
// after
var behavior = new MyBehavior { Region = newRegion };
behavior.Attach();
Defensive patterns

Strategy: validation

Validate before calling

if (behavior.IsAttached) throw new InvalidOperationException("Behavior already attached; create a new instance.");

Type guard

bool Reusable(IRegionBehavior b) => !b.IsAttached;

Try / catch

try { behavior.Region = r; } catch (InvalidOperationException) { behavior = NewBehavior(r); }

Prevention

When it happens

Trigger: Assigning behavior.Region = someRegion after behavior.Attach() has already run — commonly when a behavior instance is shared/reused across two regions.

Common situations: Reusing a singleton IRegionBehavior for multiple regions; re-registering behaviors in a custom region adapter; region re-creation reusing old behavior instances.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/8805bc077b3e7e68. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Core/Navigation/Regions/RegionBehavior.cs:26

    /// </summary>
    public abstract class RegionBehavior : IRegionBehavior
    {
        private IRegion region;

        /// <summary>
        /// Behavior's attached region.
        /// </summary>
        public IRegion Region
        {
            get
            {
                return region;
            }
            set
            {
                if (this.IsAttached)
                {
                    throw new InvalidOperationException(Resources.RegionBehaviorRegionCannotBeSetAfterAttach);
                }

                this.region = value;
            }
        }

        /// <summary>
        /// Returns <see langword="true"/> if the behavior is attached to a region, <see langword="false"/> otherwise.
        /// </summary>
        public bool IsAttached { get; private set; }

        /// <summary>
        /// Attaches the behavior to the region.
        /// </summary>
        public void Attach()
        {
            if (this.region == null)
            {

View on GitHub (pinned to 358118cd64)