AvaloniaUI/Avalonia · error · InvalidOperationException
Descendant selector must be preceded by a selector.
Error message
Descendant selector must be preceded by a selector.
What it means
Error "Descendant selector must be preceded by a selector." thrown in AvaloniaUI/Avalonia.
Source
Thrown at src/Avalonia.Base/Styling/DescendentSelector.cs:16
using System;
using Avalonia.LogicalTree;
using Avalonia.Styling.Activators;
#nullable enable
namespace Avalonia.Styling
{
internal class DescendantSelector : Selector
{
private readonly Selector _parent;
private string? _selectorString;
public DescendantSelector(Selector? parent)
{
_parent = parent ?? throw new InvalidOperationException("Descendant selector must be preceded by a selector.");
}
/// <inheritdoc/>
internal override bool IsCombinator => true;
/// <inheritdoc/>
internal override bool InTemplate => _parent.InTemplate;
/// <inheritdoc/>
internal override Type? TargetType => null;
public override string ToString(Style? owner)
{
if (_selectorString == null)
{
_selectorString = _parent.ToString(owner, true) + ' ';
}
View on GitHub (pinned to 11c5427268)
Solutions
- Ensure the selector string has a selector before the descendant combinator (a space); e.g. 'Button TextBlock' instead of starting with a space.
- Fix programmatically built selectors so a DescendentSelector is always constructed with a preceding selector.
When it happens
Trigger: Raised when a descendant selector (whitespace combinator) appears without a preceding selector in a style selector string. Defend by ensuring selector strings start with a type or name selector before any descendant combinator.
Common situations: See trigger scenarios.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/fd1a27a569e2ec78.
Report an issue: GitHub.