dotnet/wpf · error · InvalidOperationException

SR.DrawingGroup_CannotAppendToFrozenCollection

Error message

SR.DrawingGroup_CannotAppendToFrozenCollection

What it means

DrawingGroup.Close() tried to append the new drawings into a Children collection that is frozen (ImmutableFreezable). Frozen Freezables are read-only by design, so Close throws InvalidOperationException with SR.DrawingGroup_CannotAppendToFrozenCollection.

Solutions

  1. Pass a fresh, unfrozen DrawingCollection to Close
  2. Call collection.Clone() (or GetAsFrozen on a copy) to get a mutable clone before appending
  3. Remove Freeze() calls on collections that will later be reopened
  4. Do not reopen DrawingGroups whose Children live in frozen resources; build a new group instead

Example fix

// before
DrawingCollection kids = frozenTemplate.Children; // frozen
newGroup.Close(kids); // throws

// after
var kids = frozenTemplate.Children.Clone(); // mutable copy
newGroup.Close(kids);
Defensive patterns

Strategy: validation

Validate before calling

if (children != null && children.IsFrozen)
    children = children.Clone(); // mutable copy
group.Close(children);

Try / catch

try { group.Close(children); }
catch (InvalidOperationException ex) when (ex.Message.Contains("frozen"))
{
    group.Close(children.Clone());
}

Prevention

When it happens

Trigger: Calling Close(children) where children.IsFrozen is true — i.e. the DrawingCollection was frozen via Freeze() or was inherited from a frozen/sealed parent DrawingGroup.

Common situations: Reusing a frozen template collection across multiple DrawingGroups; cloning a group and trying to reuse its Children; putting a DrawingGroup in a frozen resource dictionary then reopening it.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/e299080da3426071. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/DrawingGroup.cs:122

                //
                //
                // Append the collection to the current Children collection                
                //
                //
                DrawingCollection children = Children;

                // 
                // Ensure that we can Append to the Children collection
                //
                
                if (children == null)
                {
                    throw new InvalidOperationException(SR.DrawingGroup_CannotAppendToNullCollection);                                
                }
               
                if (children.IsFrozen)
                {
                    throw new InvalidOperationException(SR.DrawingGroup_CannotAppendToFrozenCollection);                                                  
                }

                // Append the new collection to our current Children.
                //
                // TransactionalAppend rolls-back the Append operation in the event
                // an exception is thrown from the Changed event.                
                children.TransactionalAppend(rootDrawingGroupChildren);
            }            

            // This DrawingGroup is no longer open
            _open = false;
        }

        /// <summary>
        /// Calls methods on the DrawingContext that are equivalent to the
        /// Drawing with the Drawing's current value.
        /// </summary>        
        internal override void WalkCurrentValue(DrawingContextWalker ctx)

View on GitHub (pinned to 81131a70a4)