dotnet/wpf · error · InvalidOperationException

SR.RelativeSourceNeedsMode

Error message

SR.RelativeSourceNeedsMode

What it means

RelativeSource supports ISupportInitialize for XAML construction. EndInit validates the deferred state: if initialization ended with Mode still unset (IsUninitialized), it throws InvalidOperationException because a RelativeSource without a mode is unusable.

Solutions

  1. Set Mode on the RelativeSource (FindAncestor, TemplatedParent, Self, PreviousData, FindAncestor)
  2. Use the RelativeSource(mode) constructor instead of the parameterless one in code
  3. Fix the XAML so Mode is present and correctly cased

Example fix

<!-- before -->
<RelativeSource AncestorType="Window"/>
<!-- after -->
<RelativeSource Mode="FindAncestor" AncestorType="Window"/>
Defensive patterns

Strategy: validation

Validate before calling

var rs = new RelativeSource();
// ... set properties from config ...
if (rs.Mode == RelativeSourceMode.FindAncestor && rs.AncestorType == null)
    rs.AncestorType = typeof(Window);
rs.EndInit();

Try / catch

try { rs.EndInit(); }
catch (InvalidOperationException ex) { /* Mode missing — supply defaults */ }

Prevention

When it happens

Trigger: Completing ISupportInitialize on a RelativeSource created without setting Mode (e.g. XAML <RelativeSource> with no Mode attribute, then EndInit).

Common situations: XAML where the Mode attribute is misspelled or omitted on a RelativeSource element; deserialization pipelines calling EndInit without setting Mode; constructing RelativeSource via default ctor in markup extensions.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs:85

            InitializeMode(mode);
            AncestorType = ancestorType;
            AncestorLevel = ancestorLevel;
        }

#endregion constructors

#region ISupportInitialize

        /// <summary>Begin Initialization</summary>
        void ISupportInitialize.BeginInit()
        {
        }

        /// <summary>End Initialization, verify that internal state is consistent</summary>
        void ISupportInitialize.EndInit()
        {
            if (IsUninitialized)
                throw new InvalidOperationException(SR.RelativeSourceNeedsMode);
            if (_mode == RelativeSourceMode.FindAncestor && (AncestorType == null))
                throw new InvalidOperationException(SR.RelativeSourceNeedsAncestorType);
        }

#endregion ISupportInitialize

#region public properties

        /// <summary>static instance of RelativeSource for PreviousData mode.
        /// </summary>
        public static RelativeSource PreviousData
        {
            get
            {
                if (s_previousData == null)
                {
                    s_previousData = new RelativeSource(RelativeSourceMode.PreviousData);
                }

View on GitHub (pinned to 81131a70a4)