dotnet/maui · error · XamlParseException

Expected but found .

Error message

Expected {nameof(Setter)} but found {parents[4]}.

What it means

XamlParseException raised in FindTypeForVisualState when, in the Style-attached visual-state path, parents[4] is not a Setter. After confirming VisualStateGroupList at parents[3], the converter expects the value context to ultimately be rooted in a Setter; finding something else means the Setter.Property resolution cannot infer the target type from the chain.

Solutions

  1. Ensure each Setter sits directly in the Setters collection and the Property attribute is on the Setter.
  2. Re-create the VisualState/Setter markup from a known-good template.
  3. Validate the Style/Setters/VisualStateManager nesting against the docs.

Example fix

<!-- before: Setter placed where converter expects a different node -->
<VisualStateGroupList>
  <VisualStateGroup>
    <VisualState>
      <VisualState.Setters>
        <!-- mis-nested nodes -->
      </VisualState.Setters>
    </VisualState>
  </VisualStateGroup>
</VisualStateGroupList>

<!-- after -->
<VisualStateGroupList>
  <VisualStateGroup>
    <VisualState x:Name="N">
      <VisualState.Setters>
        <Setter Property="VisualElement.BackgroundColor" Value="Red" />
      </VisualState.Setters>
    </VisualState>
  </VisualStateGroup>
</VisualStateGroupList>
Defensive patterns

Strategy: validation

Validate before calling

// Keep Setter as the direct parent of the resolved Property attribute within
// VisualState.Setters.

Try / catch

try { /* compile */ }
catch (XamlParseException ex) when (ex.Message.Contains("Setter"))
{
    // verify Setter placement in the visual-state chain
}

Prevention

When it happens

Trigger: A VisualState nested in a Style's VisualStateGroupList where the immediate parent of the property reference is not a Setter; malformed Setters collection that places a Setter in the wrong slot; a Setter whose Property is being resolved outside its expected position.

Common situations: Editing Style/Setters XAML by hand and displacing a Setter; tool output that interleaves Setters with other nodes incorrectly.

Related errors


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

Appendix: source

Thrown at src/Controls/src/Core/BindablePropertyConverter.cs:149

			// Skip 0; we would not be making this check if TargetObject were not a Setter
			// Skip 1; we would not be making this check if the immediate parent were not a VisualState

			// VisualStates must be in a VisualStateGroup
			if (parents[2] is not VisualStateGroup)
				throw new XamlParseException($"Expected {nameof(VisualStateGroup)} but found {parents[2]}.", lineInfo);

			// Are these Visual States directly on a VisualElement?
			if (parents[3] is VisualElement vsTarget)
				return vsTarget.GetType();

			if (parents[3] is not VisualStateGroupList)
				throw new XamlParseException($"Expected {nameof(VisualStateGroupList)} but found {parents[3]}.", lineInfo);

			if (parents[4] is VisualElement veTarget)
				return veTarget.GetType();

			if (parents[4] is not Setter)
				throw new XamlParseException($"Expected {nameof(Setter)} but found {parents[4]}.", lineInfo);

			if (parents[5] is TriggerBase trigger)
				return trigger.TargetType;

			// These must be part of a Style; verify that 
			if (parents[5] is Style style)
				return style.TargetType;

			throw new XamlParseException($"Unable to find a TragetType for the Bindable Property. Try prefixing it with the TargetType.", lineInfo);

		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			if (value is not BindableProperty bp)
				throw new NotSupportedException();
			return $"{bp.DeclaringType.Name}.{bp.PropertyName}";
		}

View on GitHub (pinned to f377ff1c5e)