dotnet/maui · error · ArgumentException

type is not a Foregroundable type

Error message

type is not a Foregroundable type

What it means

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.

Source

Thrown at src/Compatibility/Core/src/Windows/FrameworkElementExtensions.cs:89

		}

		static DependencyProperty GetForegroundProperty(FrameworkElement element)
		{
			if (element is Control)
				return Control.ForegroundProperty;
			if (element is TextBlock)
				return TextBlock.ForegroundProperty;

			Type type = element.GetType();

			DependencyProperty foregroundProperty;
			if (!ForegroundProperties.Value.TryGetValue(type, out foregroundProperty))
			{
#pragma warning disable IL2075 // The Compatibility assembly is not trimmable
				FieldInfo field = type.GetField("ForegroundProperty", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
#pragma warning restore IL2075
				if (field == null)
					throw new ArgumentException("type is not a Foregroundable type");

				var property = (DependencyProperty)field.GetValue(null);
				ForegroundProperties.Value.TryAdd(type, property);

				return property;
			}

			return foregroundProperty;
		}

		internal static IEnumerable<T> GetChildren<T>(this DependencyObject parent) where T : DependencyObject
		{
			int myChildrenCount = VisualTreeHelper.GetChildrenCount(parent);
			for (int i = 0; i < myChildrenCount; i++)
			{
				var child = VisualTreeHelper.GetChild(parent, i);
				if (child is T)
					yield return child as T;

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Target the text-bearing element (TextBlock, Label, TextBox) instead of the container when setting foreground.
  2. If building a custom control, declare a public static ForegroundProperty DependencyProperty field.
  3. Guard the call: check element.GetField("ForegroundProperty") != null before calling SetForeground.

Example fix

// before
border.SetForegroundBinding(brush); // Border has no ForegroundProperty

// after — target the text element inside
var textBlock = border.Child as TextBlock;
if (textBlock != null)
    textBlock.SetForegroundBinding(brush);
Defensive patterns

Strategy: type-guard

Validate before calling

if (element.GetType().GetField("ForegroundProperty", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) != null)
    element.SetForegroundBinding(brush);

Type guard

static bool IsForegroundable(FrameworkElement element) =>
    element is Control ||
    element is TextBlock ||
    element.GetType().GetField("ForegroundProperty", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) != null;

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/9a81347fd37467af. Report an issue: GitHub.