{"record":{"id":"5c83faececcf10ae","repo":"AvaloniaUI/Avalonia","slug":"ancestorlevel-may-not-be-set-to-less-than-1","errorCode":null,"errorMessage":"AncestorLevel may not be set to less than 1.","messagePattern":"AncestorLevel may not be set to less than 1\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/RelativeSource.cs","lineNumber":87,"sourceCode":"        public RelativeSource(RelativeSourceMode mode)\n        {\n            Mode = mode;\n        }\n\n        /// <summary>\n        /// Gets the level of ancestor to look for when in <see cref=\"RelativeSourceMode.FindAncestor\"/>  mode.\n        /// </summary>\n        /// <remarks>\n        /// Use the default value of 1 to look for the first ancestor of the specified type.\n        /// </remarks>\n        public int AncestorLevel\n        {\n            get { return _ancestorLevel; }\n            set\n            {\n                if (value <= 0)\n                {\n                    throw new ArgumentOutOfRangeException(nameof(value), \"AncestorLevel may not be set to less than 1.\");\n                }\n\n                _ancestorLevel = value;\n            }\n        }\n\n        /// <summary>\n        /// Gets the type of ancestor to look for when in <see cref=\"RelativeSourceMode.FindAncestor\"/>  mode.\n        /// </summary>\n        public Type? AncestorType { get; set; }\n\n        /// <summary>\n        /// Gets or sets a value that describes the type of relative source lookup.\n        /// </summary>\n        public RelativeSourceMode Mode { get; set; }\n\n        public TreeType Tree { get; set; } = TreeType.Visual;\n    }","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/RelativeSource.cs#L69-L105","documentation":"RelativeSource.AncestorLevel selects which ancestor (1 = nearest) to find in FindAncestor mode. Levels below 1 are nonsensical (there is no zeroth/negative ancestor), so the setter throws ArgumentOutOfRangeException to fail fast on invalid input.","triggerScenarios":"Setting `relativeSource.AncestorLevel = 0` or any negative value. Binding it from a source that yields <=0.","commonSituations":"Off-by-one when computing ancestor levels dynamically. Defaulting a numeric variable to 0 then assigning it to AncestorLevel. XAML with AncestorLevel=0 by typo.","solutions":["Use AncestorLevel >= 1 (1 = first ancestor of AncestorType).","Clamp the source value: `relativeSource.AncestorLevel = Math.Max(1, computedLevel);`.","Fix XAML to a positive integer for AncestorLevel."],"exampleFix":"// before:\nrs.AncestorLevel = 0; // throws\n\n// after:\nrs.AncestorLevel = 1; // nearest ancestor","handlingStrategy":"validation","validationCode":"relativeSource.AncestorLevel = Math.Max(1, level);","typeGuard":"static bool IsValidLevel(int n) => n >= 1;","tryCatchPattern":null,"preventionTips":["Default AncestorLevel computations to 1, not 0.","Clamp dynamic values to >= 1.","Validate XAML integer attributes for AncestorLevel."],"tags":["bindings","relativesource","findancestor","argument-out-of-range"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}