AvaloniaUI/Avalonia · error · InvalidOperationException
Cannot set Name : styled element already styled.
Error message
Cannot set Name : styled element already styled.
What it means
Error "Cannot set Name : styled element already styled." thrown in AvaloniaUI/Avalonia.
Source
Thrown at src/Avalonia.Base/StyledElement.cs:171
/// <inheritdoc />
public event EventHandler? ActualThemeVariantChanged;
/// <summary>
/// Gets or sets the name of the styled element.
/// </summary>
/// <remarks>
/// An element's name is used to uniquely identify an element within the element's name
/// scope. Once the element is added to a logical tree, its name cannot be changed.
/// </remarks>
public string? Name
{
get => _name;
set
{
if (_stylesApplied)
{
throw new InvalidOperationException("Cannot set Name : styled element already styled.");
}
_name = value;
}
}
/// <summary>
/// Gets or sets the styled element's classes.
/// </summary>
/// <remarks>
/// <para>
/// Classes can be used to apply user-defined styling to styled elements, or to allow styled elements
/// that share a common purpose to be easily selected.
/// </para>
/// </remarks>
public Classes Classes => _classes ??= new();
/// <summary>View on GitHub (pinned to 11c5427268)
Solutions
- Set the Name property before the element is styled (before it is added to the visual tree and templates are applied).
- If the name must change at runtime, unregister and re-register it in the appropriate NameScope instead of setting Name directly.
When it happens
Trigger: Raised when the Name property of a styled element is assigned after styling has begun. Defend by setting Name before the element is initialized/added to the tree, and guarding setters against post-init assignment.
Common situations: See trigger scenarios.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/29083c0b629c25b3.
Report an issue: GitHub.