{"record":{"id":"e1a9c4de35ef614f","repo":"lepoco/wpfui","slug":"expected-either-typeof-iconsourceelement-or","errorCode":null,"errorMessage":"Expected either '{typeof(IconSourceElement)}' or '{typeof(IconElement)}' but got '{baseValue.GetType()}'.","messagePattern":"Expected either '(.+?)' or '(.+?)' but got '(.+?)'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/IconElement/IconElement.cs","lineNumber":108,"sourceCode":"        EnsureLayoutRoot();\n\n        _layoutRoot!.Arrange(new Rect(default, finalSize));\n        return finalSize;\n    }\n\n    /// <summary>\n    /// Coerces the value of an Icon dependency property, allowing the use of either IconElement or IconSourceElement.\n    /// </summary>\n    /// <param name=\"_\">The dependency object (unused).</param>\n    /// <param name=\"baseValue\">The value to be coerced.</param>\n    /// <returns>An IconElement, either directly or derived from an IconSourceElement.</returns>\n    public static object? Coerce(DependencyObject _, object? baseValue)\n    {\n        return baseValue switch\n        {\n            IconSourceElement iconSourceElement => iconSourceElement.CreateIconElement(),\n            IconElement or null => baseValue,\n            _ => throw new ArgumentException(\n                message: $\"Expected either '{typeof(IconSourceElement)}' or '{typeof(IconElement)}' but got '{baseValue.GetType()}'.\",\n                paramName: nameof(baseValue)\n            ),\n        };\n    }\n}\n","sourceCodeStart":90,"sourceCodeEnd":115,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/IconElement/IconElement.cs#L90-L115","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Wrap the value: assign an IconSourceElement whose IconSource is the appropriate source, or a concrete IconElement subtype.","If you have a Symbol/geometry, use the dedicated icon type (e.g. SymbolIcon, FontIcon) rather than the raw value.","Ensure bindings produce IconElement or IconSourceElement values; convert in an IValueConverter if the source type differs.","Check the XAML: the element inside an Icon property must be one of the supported icon types."],"exampleFix":"<!-- before -->\n<ui:Button Icon=\"{Binding IconGeometry}\"/> <!-- geometry -> throws in Coerce -->\n\n<!-- after -->\n<ui:Button>\n  <ui:Button.Icon>\n    <ui:SymbolIcon Symbol=\"Fluent24\"/>\n  </ui:Button.Icon>\n</ui:Button>","handlingStrategy":"type-guard","validationCode":"object? value = GetValueFromBinding();\nif (value is not null and not IconElement and not IconSourceElement)\n    throw new InvalidOperationException($\"Icon must be IconElement/IconSourceElement, got {value.GetType()}\");","typeGuard":"static bool IsValidIconValue(object? v) => v is null or IconElement or IconSourceElement;","tryCatchPattern":null,"preventionTips":["Only assign IconElement, IconSourceElement, or null to Icon properties.","Use an IValueConverter when binding from a non-icon source type.","Validate bound values in DEBUG before assignment."],"tags":["wpf","iconelement","coerce","type-mismatch","xaml"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}