dotnet/wpf · error · ArgumentException

SR.Format(SR.NonWhiteSpaceInAddText, s)

Error message

SR.Format(SR.NonWhiteSpaceInAddText, s)

What it means

XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText throws ArgumentException when a string containing non-whitespace characters is passed to AddText on an element that cannot accept text content. WPF calls it from AddText implementations of content-less controls to reject stray text inside their XAML tags.

Solutions

  1. Remove the text between the element's open/close tags, or wrap it in a valid content element (e.g. <TextBlock Text="..."/>)
  2. Check for invisible non-whitespace characters (U+00A0, zero-width space, BOM) — retype the whitespace or re-save the XAML as clean UTF-8
  3. If the control should accept text, switch to a control type that supports text content (TextBlock, ContentControl with string content)

Example fix

// before
<Grid>
  Leftover text
</Grid>
// after
<Grid>
  <TextBlock Text="Leftover text"/>
</Grid>
Defensive patterns

Strategy: validation

Validate before calling

bool isWhitespaceOnly = string.IsNullOrEmpty(s) || s.All(char.IsWhiteSpace);

Type guard

static bool IsWhiteSpaceOnly(string s) => string.IsNullOrEmpty(s) || s.All(char.IsWhiteSpace);

Try / catch

try { serializerUtil.ThrowIfNonWhiteSpaceInAddText(text); }
catch (ArgumentException ex) { /* strip or route non-whitespace text */ }

Prevention

When it happens

Trigger: Putting text (or a typo'd character) directly inside the element tags of a control whose AddText forbids content, e.g. <Grid>abc</Grid> or a non-breaking space/hidden character in a container's XAML.

Common situations: Stray whitespace-looking characters like &nbsp; (U+00A0), BOM or zero-width characters pasted into XAML between container tags; leftover text after refactoring a control from a TextBlock to a content-less panel; invalid children placed as raw text.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Markup/XamlSerializerUtil.cs:56

                throw new InvalidOperationException();
            }
        }

        /// <summary>
        ///     Throw an exception if the passed string is not empty and is not
        ///     all whitespace.  This is used to check IAddChild.AddText calls for
        ///     objects that don't handle text, but may get some whitespace if
        ///     if xml:space="preserve" is set in xaml.
        /// </summary>
        internal static void ThrowIfNonWhiteSpaceInAddText(string s)
        {
            if (s != null)
            {
                for (int i = 0; i < s.Length; i++)
                {
                    if (!Char.IsWhiteSpace(s[i]))
                    {
                        throw new ArgumentException(SR.Format(SR.NonWhiteSpaceInAddText, s));
                    }
                }
            }
        }
    }
}

View on GitHub (pinned to 81131a70a4)