dotnet/wpf · error · InvalidOperationException

SR.AddText_Invalid

Error message

SR.AddText_Invalid

What it means

ModelVisual3D overrides AddText (from the IAddChild pattern) and only accepts strings consisting entirely of whitespace, which it silently ignores. Any string containing a non-whitespace character causes this InvalidOperationException. Visual3D content in WPF 3D cannot be specified as text, so the type deliberately rejects AddText calls.

Solutions

  1. Remove the text content from the ModelVisual3D element; 3D visuals have no text content.
  2. Set content via Model property instead: assign a GeometryModel3D / Model3DGroup to ModelVisual3D.Model.
  3. To display text in 3D, use a 3D Text (e.g. Text3D via converted Geometry from FormattedText applied to a GeometryModel3D).
  4. If the text is only whitespace, it is ignored and no exception occurs; strip or trim non-whitespace before calling AddText.

Example fix

// before
var v = new ModelVisual3D();
v.AddText("hello"); // InvalidOperationException
// after
var v = new ModelVisual3D();
v.Model = new GeometryModel3D(geometry, material); // use Model, not text
Defensive patterns

Strategy: validation

Validate before calling

if (!string.IsNullOrWhiteSpace(text)) throw new InvalidOperationException("ModelVisual3D does not accept text content; use the Model property."); // else safe to call AddText
v.AddText(text);

Type guard

static bool CanAddText(string text) => text == null || text.All(char.IsWhiteSpace);

Try / catch

try { v.AddText(text); }
catch (InvalidOperationException ex) when (ex.Message.Contains("AddText"))
{
    // fall back to Model-based content
}

Prevention

When it happens

Trigger: Calling ModelVisual3D.AddText(string) (directly or via XAML text content) where text contains any non-whitespace character, e.g. addText("hello").

Common situations: Putting literal text content inside a <ModelVisual3D> element in XAML (the XAML loader invokes IAddChild.AddText); copy-pasting markup patterns from 2D elements like TextBlock into 3D; generating XAML programmatically with text inside ModelVisual3D.

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/3ba13d2206751d94. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/ModelVisual3D.cs:92

            Visual3D visual3D = value as Visual3D;

            if (visual3D == null)
            {
                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, nameof(Visual3D)));
            }

            Children.Add(visual3D);
        }

        void IAddChild.AddText(string text)
        {
            // The only text we accept is whitespace, which we ignore.
            foreach (char c in text)
            {
                if (!Char.IsWhiteSpace(c))
                {
                    throw new System.InvalidOperationException(SR.Format(SR.AddText_Invalid, this.GetType().Name));
                }
            }
        }

        /// <summary>
        ///     Children of this Visual3D
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Visual3DCollection Children
        {
            get
            {
                VerifyAPIReadOnly();

                return _children;
            }
        }

View on GitHub (pinned to 81131a70a4)