{"record":{"id":"9a81347fd37467af","repo":"dotnet/maui","slug":"type-is-not-a-foregroundable-type","errorCode":null,"errorMessage":"type is not a Foregroundable type","messagePattern":"type is not a Foregroundable type","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Windows/FrameworkElementExtensions.cs","lineNumber":89,"sourceCode":"\t\t}\n\n\t\tstatic DependencyProperty GetForegroundProperty(FrameworkElement element)\n\t\t{\n\t\t\tif (element is Control)\n\t\t\t\treturn Control.ForegroundProperty;\n\t\t\tif (element is TextBlock)\n\t\t\t\treturn TextBlock.ForegroundProperty;\n\n\t\t\tType type = element.GetType();\n\n\t\t\tDependencyProperty foregroundProperty;\n\t\t\tif (!ForegroundProperties.Value.TryGetValue(type, out foregroundProperty))\n\t\t\t{\n#pragma warning disable IL2075 // The Compatibility assembly is not trimmable\n\t\t\t\tFieldInfo field = type.GetField(\"ForegroundProperty\", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);\n#pragma warning restore IL2075\n\t\t\t\tif (field == null)\n\t\t\t\t\tthrow new ArgumentException(\"type is not a Foregroundable type\");\n\n\t\t\t\tvar property = (DependencyProperty)field.GetValue(null);\n\t\t\t\tForegroundProperties.Value.TryAdd(type, property);\n\n\t\t\t\treturn property;\n\t\t\t}\n\n\t\t\treturn foregroundProperty;\n\t\t}\n\n\t\tinternal static IEnumerable<T> GetChildren<T>(this DependencyObject parent) where T : DependencyObject\n\t\t{\n\t\t\tint myChildrenCount = VisualTreeHelper.GetChildrenCount(parent);\n\t\t\tfor (int i = 0; i < myChildrenCount; i++)\n\t\t\t{\n\t\t\t\tvar child = VisualTreeHelper.GetChild(parent, i);\n\t\t\t\tif (child is T)\n\t\t\t\t\tyield return child as T;","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Windows/FrameworkElementExtensions.cs#L71-L107","documentation":"Thrown as ArgumentException by FrameworkElementExtensions.GetForegroundProperty when the element is not a Control, not a TextBlock, and its type does not expose a public static 'ForegroundProperty' field via reflection. The extension tries reflection as a fallback to find the dependency property for foreground binding; if none exists, the type is inherently non-foregroundable.","triggerScenarios":"Calling SetForeground or a binding helper on a FrameworkElement that has no Foreground dependency property — e.g., a Panel (Grid, StackPanel), Shape without ForegroundProperty, or a custom element that doesn't declare one.","commonSituations":"Applying foreground styling to a container element like Grid or Border instead of to the text-bearing child (TextBlock/Label). Also when a custom control forgets to declare ForegroundProperty.","solutions":["Target the text-bearing element (TextBlock, Label, TextBox) instead of the container when setting foreground.","If building a custom control, declare a public static ForegroundProperty DependencyProperty field.","Guard the call: check element.GetField(\"ForegroundProperty\") != null before calling SetForeground."],"exampleFix":"// before\nborder.SetForegroundBinding(brush); // Border has no ForegroundProperty\n\n// after — target the text element inside\nvar textBlock = border.Child as TextBlock;\nif (textBlock != null)\n    textBlock.SetForegroundBinding(brush);","handlingStrategy":"type-guard","validationCode":"if (element.GetType().GetField(\"ForegroundProperty\", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) != null)\n    element.SetForegroundBinding(brush);","typeGuard":"static bool IsForegroundable(FrameworkElement element) =>\n    element is Control ||\n    element is TextBlock ||\n    element.GetType().GetField(\"ForegroundProperty\", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) != null;","tryCatchPattern":null,"preventionTips":["Apply foreground styling to text-bearing elements, not containers.","Declare a ForegroundProperty field on custom controls that should support foreground binding."],"tags":["wpf","maui-compatibility","foreground","dependencyproperty","reflection"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}