{"record":{"id":"14072920bf640080","repo":"lepoco/wpfui","slug":"use-nameof-createiconelement","errorCode":null,"errorMessage":"Use {nameof(CreateIconElement)}","messagePattern":"Use \\{nameof\\(CreateIconElement\\)\\}","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/IconElement/IconSourceElement.cs","lineNumber":37,"sourceCode":"        nameof(IconSource),\n        typeof(IconSource),\n        typeof(IconSourceElement),\n        new FrameworkPropertyMetadata(null)\n    );\n\n    /// <summary>\n    /// Gets or sets <see cref=\"IconSource\"/>\n    /// </summary>\n    public IconSource? IconSource\n    {\n        get => (IconSource?)GetValue(IconSourceProperty);\n        set => SetValue(IconSourceProperty, value);\n    }\n\n    protected override UIElement InitializeChildren()\n    {\n        // TODO: Come up with an elegant solution\n        throw new InvalidOperationException($\"Use {nameof(CreateIconElement)}\");\n    }\n\n    public IconElement? CreateIconElement()\n    {\n        return IconSource?.CreateIconElement();\n    }\n}\n","sourceCodeStart":19,"sourceCodeEnd":45,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/IconElement/IconSourceElement.cs#L19-L45","documentation":"Thrown by IconSourceElement.InitializeChildren, which is invoked by IconElement.EnsureLayoutRoot during measure/arrange. IconSourceElement deliberately does not render children itself — it is a source wrapper whose only valid output path is CreateIconElement(), which materializes the inner IconSource into a real IconElement. The throw is a usage error marker: someone placed an IconSourceElement into the visual tree directly instead of using it as an Icon property value.","triggerScenarios":"Putting an <ui:IconSourceElement> directly into a panel/grid's children so WPF measures and arranges it; or subclassing and calling InitializeChildren manually. The coerce path (CreateIconElement) is bypassed because the element is treated as a standalone control rather than as an icon source.","commonSituations":"Confusing IconSource (data) with an icon control (visual); XAML that nests IconSourceElement as a child element of a layout panel instead of assigning it to an Icon property; binding a ContentControl's Content to an IconSourceElement.","solutions":["Use IconSourceElement only as the value of an Icon dependency property — it will be coerced via CreateIconElement.","If you need a visual icon, assign an IconSourceElement.IconSource or use a concrete IconElement directly.","Do not add IconSourceElement to Children collections of panels.","In bindings, ensure the target property is an Icon property, not Content/Child."],"exampleFix":"<!-- before -->\n<StackPanel>\n  <ui:IconSourceElement IconSource=\"...\"/> <!-- measured -> throws -->\n</StackPanel>\n\n<!-- after -->\n<ui:Button>\n  <ui:Button.Icon>\n    <ui:IconSourceElement IconSource=\"...\"/>\n  </ui:Button.Icon>\n</ui:Button>","handlingStrategy":"validation","validationCode":"// Never place IconSourceElement in a panel's Children.\n// Only assign it to an Icon dependency property:\nbutton.Icon = new IconSourceElement { IconSource = source };","typeGuard":"static bool IsAssignableToIconProperty(object? v) => v is null or IconElement or IconSourceElement;","tryCatchPattern":null,"preventionTips":["Treat IconSourceElement as a value for Icon properties, never as a standalone control.","Do not add it to StackPanel/Grid.Children.","Do not bind ContentControl.Content to an IconSourceElement."],"tags":["wpf","iconelement","misuse","visual-tree","xaml"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}