dotnet/wpf · error · ArgumentException

SR.Format(SR.UnexpectedParameterType, value.GetType()…

Error message

SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(StoryFragment))

What it means

This ArgumentException is thrown by StoryFragments.AddChild when the child passed is not a StoryFragment. StoryFragments is the root of the XPS story structure and only accepts StoryFragment children.

Solutions

  1. Create a StoryFragment, add block elements to it, then add the fragment to StoryFragments
  2. Keep SectionStructure/ParagraphStructure/etc. inside StoryFragment, not StoryFragments
  3. Verify the child's runtime type from the message
  4. Type-check with 'is StoryFragment' before adding

Example fix

// before
storyFragments.AddChild(new SectionStructure());
// after
var fragment = new StoryFragment();
fragment.BlockElements.Add(new SectionStructure());
storyFragments.AddChild(fragment);
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not StoryFragment) throw new ArgumentException("StoryFragments.AddChild only accepts StoryFragment");

Type guard

bool IsFragment(object o) => o is StoryFragment;

Try / catch

try { fragments.AddChild(value); } catch (ArgumentException ex) { /* log and wrap in a fragment instead */ }

Prevention

When it happens

Trigger: Calling AddChild on a StoryFragments instance with any object other than StoryFragment — e.g. adding SectionStructure or ParagraphStructure at the root level.

Common situations: Misreading the story hierarchy and adding block elements directly to the StoryFragments root; XAML with a structure element as a direct child of StoryFragments.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentStructures/StoryFragments.cs:46

        public void Add(StoryFragment storyFragment)
        {
            ArgumentNullException.ThrowIfNull(storyFragment);

            ((IAddChild) this).AddChild(storyFragment);
        }

        void IAddChild.AddChild(object value)
        {
            //
            // Only the StoryFragment type are accepted. 
            //
            if (value is StoryFragment)
            {
                _elementList.Add( (StoryFragment) value);
                return;
            }

            throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(StoryFragment)), nameof(value));
        }
        
        void IAddChild.AddText(string text) { } 
        
        IEnumerator<StoryFragment> IEnumerable<StoryFragment>.GetEnumerator()
        {
            throw new NotSupportedException();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<StoryFragment>)this).GetEnumerator();
        }
        
        internal List<StoryFragment> StoryFragmentList
        {
            get
            {

View on GitHub (pinned to 81131a70a4)