dotnet/wpf · error · InvalidOperationException
SR.RelativeSourceNeedsAncestorType
Error message
SR.RelativeSourceNeedsAncestorType
What it means
EndInit validation for RelativeSource also requires that FindAncestor mode specifies an AncestorType. A RelativeSource in FindAncestor mode with a null AncestorType throws InvalidOperationException — the engine would not know which ancestor type to walk to.
Solutions
- Set AncestorType (e.g. typeof(Window)) when using FindAncestor mode
- Add AncestorType= in the XAML element
- Switch to a mode that does not need AncestorType if ancestor lookup is not intended
Example fix
// before
var rs = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorLevel = 1 }; // EndInit throws
// after
var rs = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorLevel = 1, AncestorType = typeof(Window) }; Defensive patterns
Strategy: validation
Validate before calling
if (rs.Mode == RelativeSourceMode.FindAncestor && rs.AncestorType == null)
throw new InvalidOperationException("FindAncestor requires AncestorType"); Prevention
- Pair AncestorLevel with AncestorType in FindAncestor mode
- Add AncestorType in XAML whenever Mode="FindAncestor"
- Use x:Static/type references carefully so AncestorType resolves
When it happens
Trigger: Calling ISupportInitialize.EndInit when _mode == RelativeSourceMode.FindAncestor and AncestorType is null.
Common situations: XAML <RelativeSource Mode="FindAncestor" AncestorLevel="1"/> missing AncestorType; code building a FindAncestor RelativeSource but forgetting the type.
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
- SR.RelativeSourceInvalidAncestorLevel
- SR.RelativeSourceModeIsImmutable
- SR.RelativeSourceNeedsMode
- SR.RelativeSourceNotInFindAncestorMode
- ArgumentNullException(nameof(value))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e426f3493625fa81.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs:87
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);
}
return s_previousData;
}View on GitHub (pinned to 81131a70a4)