dotnet/wpf · error · ArgumentException

SR.Collection_BadType (Drawing)

Error message

SR.Collection_BadType (Drawing)

What it means

DrawingCollection.Cast (called from Add/Insert) rejects any value that is not a Drawing instance, throwing ArgumentException with SR.Collection_BadType naming the collection type, the value's actual type, and the expected type. WPF freezable collections are strongly typed even where object-based IList APIs let weakly-typed values slip in.

Solutions

  1. Wrap the object in a Drawing-derived type first, e.g. use a GeometryDrawing/ ImageDrawing that references the Brush or ImageSource you meant to add.
  2. Check the runtime type: the value must derive from System.Windows.Media.Drawing; fix the variable's declared type or the code that constructs it.
  3. If using the IList (non-generic) API, cast or validate with `value is Drawing` before Add/Insert.

Example fix

// before
drawingCollection.Add(myBrush); // ArgumentException
// after
drawingCollection.Add(new GeometryDrawing { Brush = myBrush, Geometry = myGeometry });
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not System.Windows.Media.Drawing d) throw new ArgumentException($"Expected Drawing, got {value?.GetType().Name}"); collection.Add(d);

Type guard

static bool IsDrawing(object v) => v is System.Windows.Media.Drawing;

Try / catch

try { collection.Add(value); } catch (ArgumentException ex) when (ex.Message.Contains("Drawing")) { /* log type mismatch, substitute correct Drawing */ }

Prevention

When it happens

Trigger: Calling DrawingCollection.Add(object), Insert(int, object), or the IList indexer setter with a non-Drawing object (e.g. an Image, Brush, Geometry, or a custom element that is not a Drawing subclass).

Common situations: Mixing up WPF graphics types: adding a Brush or Geometry where a Drawing is expected; using the non-generic IList interface from reflection, XAML markup extensions, or databinding code that boxes values; porting code from DrawingContext calls to collection Adds.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DrawingCollection.cs:518

                DependencyObject inheritanceChild = _collection[i];
                if (inheritanceChild != null && inheritanceChild.InheritanceContext == this)
                {
                    inheritanceChild.OnInheritanceContextChanged(args);
                }
            }
        }

        #endregion

        #region Private Helpers

        private Drawing Cast(object value)
        {
            ArgumentNullException.ThrowIfNull(value);

            if (!(value is Drawing))
            {
                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, "Drawing"));
            }

            return (Drawing) value;
        }

        // IList.Add returns int and IList<T>.Add does not. This
        // is called by both Adds and IList<T>'s just ignores the
        // integer
        private int AddHelper(Drawing value)
        {
            int index = AddWithoutFiringPublicEvents(value);

            // AddAtWithoutFiringPublicEvents incremented the version

            WritePostscript();

            return index;
        }

View on GitHub (pinned to 81131a70a4)