AvaloniaUI/Avalonia · error · InvalidOperationException

Cannot subscribe to IndexerDescriptor.

Error message

Cannot subscribe to IndexerDescriptor.

What it means

IndexerDescriptor represents an indexer-based property accessor built via the Avalonia `this[...]`/PropertyPath syntax. Subscribe requires either a SourceObservable or a Source object to pull values from; with neither, there is nothing to subscribe to, so it throws InvalidOperationException.

Source

Thrown at src/Avalonia.Base/Data/IndexerDescriptor.cs:109

            return this;
        }

        /// <summary>
        /// Modifies the binding priority.
        /// </summary>
        /// <param name="priority">The binding priority.</param>
        /// <returns>The object that the method was called on.</returns>
        public IndexerDescriptor WithPriority(BindingPriority priority)
        {
            Priority = priority;
            return this;
        }

        /// <inheritdoc/>
        public IDisposable Subscribe(IObserver<object?> observer)
        {
            if (SourceObservable is null && Source is null)
                throw new InvalidOperationException("Cannot subscribe to IndexerDescriptor.");
            if (Property is null)
                throw new InvalidOperationException("Cannot subscribe to IndexerDescriptor.");

            return (SourceObservable ?? Source!.GetObservable(Property)).Subscribe(observer);
        }
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Assign a Source AvaloniaObject to the descriptor before subscribing, e.g. `descriptor.Source = myControl;`.
  2. Or set SourceObservable to provide the value stream directly.
  3. Construct the descriptor via `control[...]` on a non-null control so Source is populated automatically.

Example fix

// before:
var d = someControl[ItemsControl.ItemsProperty];
d.Source = null;
d.Subscribe(obs); // throws

// after:
d.Source = someControl;
d.Subscribe(obs);
Defensive patterns

Strategy: validation

Validate before calling

if (descriptor.Source is null && descriptor.SourceObservable is null)
    throw new InvalidOperationException("Set descriptor.Source or SourceObservable.");
descriptor.Subscribe(observer);

Type guard

static bool HasSource(IndexerDescriptor d) => d.Source is not null || d.SourceObservable is not null;

Prevention

When it happens

Trigger: Calling `descriptor.Subscribe(observer)` on an IndexerDescriptor created via the indexer operator without ever assigning `.Source` or setting a source observable (e.g. `control[ItemsControl.Items]` used standalone without a source).

Common situations: Building a property path/indexer descriptor for binding but forgetting to set the source. Passing a partially-constructed IndexerDescriptor to a subscriber or test.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/c1c47eb9076de739. Report an issue: GitHub.