dotnet/wpf · error · InvalidOperationException

SR.RelativeSourceNotInFindAncestorMode

Error message

SR.RelativeSourceNotInFindAncestorMode

What it means

Setting AncestorType on a RelativeSource whose Mode is not FindAncestor throws this InvalidOperationException. AncestorType is only meaningful in FindAncestor mode; once Mode has been set explicitly to another value (e.g. PreviousData, TemplatedParent, Self), assigning AncestorType is an invalid configuration, so the setter rejects it rather than silently ignoring the type.

Solutions

  1. Set Mode="FindAncestor" or remove the AncestorType attribute
  2. Set AncestorType only after Mode is FindAncestor
  3. Delete leftover AncestorType/AncestorLevel attributes in non-FindAncestor RelativeSource elements

Example fix

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

Strategy: validation

Validate before calling

if (rs.Mode != RelativeSourceMode.FindAncestor)
    rs.AncestorType = null; // clear before changing mode

Prevention

When it happens

Trigger: Assigning RelativeSource.AncestorType != null when _mode is Self, TemplatedParent, or PreviousData.

Common situations: Copy-pasted XAML keeping an AncestorType attribute while changing Mode to Self/TemplatedParent; code that sets AncestorType before Mode and had Mode resolved to something other than FindAncestor.

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

Appendix: source

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

        /// </summary>
        /// <remarks> if Mode has not been set explicitly, setting AncestorType will implicitly lock Mode to FindAncestor. </remarks>
        /// <exception cref="InvalidOperationException"> RelativeSource is not in FindAncestor mode </exception>
        public Type AncestorType
        {
            get { return _ancestorType; }
            set
            {
                if (IsUninitialized)
                {
                    Debug.Assert(_mode == RelativeSourceMode.FindAncestor);
                    AncestorLevel = 1;  // lock the mode and set default level
                }

                if (_mode != RelativeSourceMode.FindAncestor)
                {
                    // in all other modes, AncestorType should not get set to a non-null value
                    if (value != null)
                        throw new InvalidOperationException(SR.RelativeSourceNotInFindAncestorMode);
                }
                else
                {
                    _ancestorType = value;
                }
            }
        }

        /// <summary>
        /// This method is used by TypeDescriptor to determine if this property should
        /// be serialized.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeAncestorType()
        {
            return (_mode == RelativeSourceMode.FindAncestor);
        }

View on GitHub (pinned to 81131a70a4)