Skip to content

[XSG] MAUIG2045 false positive for RelativeSource AncestorType bindings with derived ancestor types (regression from #34408) #36818

Description

@kubaflo

Summary

#34408 ("[XAML] SourceGen: Compile RelativeSource AncestorType bindings under AOT") introduced a false-positive MAUIG2045 diagnostic for RelativeSource AncestorType bindings whose runtime ancestor is a derived type.

Repro

src/Controls/samples/Controls.Sample — e.g. Pages/ControlsPage.xaml(49,14):

SelectedItem="{Binding SelectedItem, Source={x:RelativeSource AncestorType={x:Type ContentPage}}, Mode=TwoWay}"
error MAUIG2045: Binding: Property "SelectedItem" not found on
"global::Microsoft.Maui.Controls.ContentPage". The binding will use slower
reflection-based binding at runtime.

The XAML root is views:BasePage, and BasePage : ContentPage declares both NavigateCommand and SelectedItem. At runtime the resolved ancestor is a BasePage, so the binding is valid — the diagnostic is wrong.

23 diagnostics across 12 XAML files in that one sample.

Cause

src/Controls/src/SourceGen/KnownMarkups.cs (added in #34408) resolves TryGetRelativeSourceAncestorType(...) and then does dataTypeSymbol = ancestorTypeSymbol, validating the property against the declared AncestorType. Because AncestorType matching is an is-style check, any derived type is a legal runtime match, so absence of the property on the declared type does not imply the binding is broken.

Impact

Fires for any consumer using RelativeSource AncestorType with a derived page/view type — a very common pattern. MAUIG2045 is declared Warning severity (src/Controls/src/SourceGen/AnalyzerReleases.Unshipped.md:24), so it only breaks builds where warnings are errors — which is exactly what SampleTests.cs:39 does (TreatWarningsAsErrors=true).

CI evidence

Regressed the Samples windows and Samples macOS integration-test legs on net11.0:

Build Commit Samples windows Samples macOS
1527085 81ac35f
1527120 9f6a292
1527321 bbcba3f (#34408)

Failing test: Microsoft.Maui.IntegrationTests.SampleTests.Build(relativeProj: "src\Controls\samples\Controls.Sample\Maui.Controls.Sample.csproj", config: "Debug"|"Release").

Current mitigation (must be reverted)

MAUIG2045 was added to <NoWarn> in Maui.Controls.Sample.csproj to unblock the .NET 11 Preview 7 cut. This only hides it in our sample — customers still hit the false positive. Remove the NoWarn entry as part of the real fix.

Suggested fix

Suppress BindingPropertyNotFound when the property is absent from the AncestorType but that type is unsealed, since a derived runtime ancestor may declare it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-xamlXAML, CSS, Triggers, Behaviorspartner/syncfusionIssues / PR's with Syncfusion collaborationplatform/macosmacOS / Mac Catalystplatform/windowss/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triage

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions