lepoco/wpfui · error · ArgumentException

Expected either '{typeof(IconSourceElement)}' or '{typeof(Ic

Error message

Expected either '{typeof(IconSourceElement)}' or '{typeof(IconElement)}' but got '{baseValue.GetType()}'.

What it means

Thrown by IconElement.Coerce (a dependency-property coerce callback) when the value being assigned to an Icon property is neither an IconElement, an IconSourceElement, nor null. Coerce is the gatekeeper that normalizes icon values; anything else (a string, an ImageSource, a random object) is rejected because the icon rendering pipeline cannot interpret it.

Source

Thrown at src/Wpf.Ui/Controls/IconElement/IconElement.cs:108

        EnsureLayoutRoot();

        _layoutRoot!.Arrange(new Rect(default, finalSize));
        return finalSize;
    }

    /// <summary>
    /// Coerces the value of an Icon dependency property, allowing the use of either IconElement or IconSourceElement.
    /// </summary>
    /// <param name="_">The dependency object (unused).</param>
    /// <param name="baseValue">The value to be coerced.</param>
    /// <returns>An IconElement, either directly or derived from an IconSourceElement.</returns>
    public static object? Coerce(DependencyObject _, object? baseValue)
    {
        return baseValue switch
        {
            IconSourceElement iconSourceElement => iconSourceElement.CreateIconElement(),
            IconElement or null => baseValue,
            _ => throw new ArgumentException(
                message: $"Expected either '{typeof(IconSourceElement)}' or '{typeof(IconElement)}' but got '{baseValue.GetType()}'.",
                paramName: nameof(baseValue)
            ),
        };
    }
}

View on GitHub (pinned to ffebacd610)

Solutions

  1. Wrap the value: assign an IconSourceElement whose IconSource is the appropriate source, or a concrete IconElement subtype.
  2. If you have a Symbol/geometry, use the dedicated icon type (e.g. SymbolIcon, FontIcon) rather than the raw value.
  3. Ensure bindings produce IconElement or IconSourceElement values; convert in an IValueConverter if the source type differs.
  4. Check the XAML: the element inside an Icon property must be one of the supported icon types.

Example fix

<!-- before -->
<ui:Button Icon="{Binding IconGeometry}"/> <!-- geometry -> throws in Coerce -->

<!-- after -->
<ui:Button>
  <ui:Button.Icon>
    <ui:SymbolIcon Symbol="Fluent24"/>
  </ui:Button.Icon>
</ui:Button>
Defensive patterns

Strategy: type-guard

Validate before calling

object? value = GetValueFromBinding();
if (value is not null and not IconElement and not IconSourceElement)
    throw new InvalidOperationException($"Icon must be IconElement/IconSourceElement, got {value.GetType()}");

Type guard

static bool IsValidIconValue(object? v) => v is null or IconElement or IconSourceElement;

Prevention

When it happens

Trigger: Assigning a non-icon value to a dependency property whose coerce callback is IconElement.Coerce — e.g. binding a string path, a geometry, or a System.Windows.Media.ImageSource directly to an Icon property; XAML that places an unsupported element inside an Icon slot.

Common situations: Migrating from a different icon library and assigning the old value type (geometry/string/URI) directly; data-binding an Icon property to a model whose type is not an IconElement/IconSourceElement; XAML typo putting the wrong element in the Icon property.

Related errors


AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13). Data as JSON: /api/errors/e1a9c4de35ef614f. Report an issue: GitHub.