Skip to content

[XSG] Fix RelativeSource binding with FindAncestorBindingContext mode - #32925

Merged
StephaneDelcroix merged 4 commits into
mainfrom
copilot/fix-relative-source-binding
Dec 1, 2025
Merged

StephaneDelcroix merged 4 commits into
mainfrom
copilot/fix-relative-source-binding

Conversation

Copilot AI commented Nov 30, 2025 •

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description of Change

RelativeSourceExtension was not registered in the source generator's known markup extensions, causing bindings with {RelativeSource AncestorType=..., Mode=FindAncestorBindingContext} to fail at runtime when XSG is enabled.

Changes:

  • Add ProvideValueForRelativeSourceExtension in KnownMarkups.cs to generate correct code for all RelativeSource modes (Self, TemplatedParent, FindAncestor, FindAncestorBindingContext)
  • Register RelativeSourceExtension in GetKnownEarlyMarkupExtensions
  • Automatic mode inference when Mode not specified: Element types → FindAncestor, non-Element types → FindAncestorBindingContext
  • Add Maui32924 Xaml.UnitTests to validate the fix across all inflators (Runtime, XamlC, SourceGen)
  • Add RelativeSourceBindings SourceGen.UnitTests to verify generated code does not contain XamlTypeResolver (which would produce trimming and AOT warnings)

Example that now works with XSG:

<Label Text="{Binding Path=Name, Source={RelativeSource AncestorType={x:Type vm:MyViewModel}, Mode=FindAncestorBindingContext}}" />

Issues Fixed

Fixes #32924

Original prompt

This section details on the original issue you should resolve

<issue_title>[XSG] RelativeSource binding with FindAncestorBindingContext does not work</issue_title>
<issue_description>### Description

From Discord:

works in aot release mode but not with the source generator enabled:

Command="{Binding Path=ToggleTorchCommand, Source={RelativeSource AncestorType={x:Type 
vm:ScanViewModel}, Mode=FindAncestorBindingContext}}"

It appears that XSG code does not correctly generate the code of the binding.

I also noticed we don't have Xaml.UnitTests scenarios with FindAncestorBindingContext. We need to extend our test coverage to ensure that all inflators behave the same.

Note: in the example above, the x:DataType is missing. We should make sure that XSG produces working code with and without x:DataType. Only the one with x:DataType is expected to not produce any trimming warnings.

Steps to Reproduce

TBD

Link to public reproduction project repository

No response

Version with bug

10.0.11

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

I was not able test on other platforms

Affected platform versions

No response

Did you find any workaround?

Works with XamlC.

Relevant log output

```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 30, 2025 21:48
…text mode

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
…ed test

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix RelativeSource binding with FindAncestorBindingContext [XSG] Fix RelativeSource binding with FindAncestorBindingContext mode Nov 30, 2025
Copilot AI requested a review from simonrozsival November 30, 2025 22:00

@simonrozsival simonrozsival left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot looks great! I'd like to also add a SourceGen.UnitTests which will make sure that the generated code for the XAML just as Maui32924.xaml does not contain XamlTypeResolver (which would produce trimming and AOT warnings).

…TypeResolver usage

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>

Copilot AI commented Nov 30, 2025

Copy link
Copy Markdown
Contributor Author

@copilot looks great! I'd like to also add a SourceGen.UnitTests which will make sure that the generated code for the XAML just as Maui32924.xaml does not contain XamlTypeResolver (which would produ...

Added RelativeSourceBindings.cs in SourceGen.UnitTests with 5 tests that verify the generated code does not contain XamlTypeResolver. The tests cover FindAncestorBindingContext, FindAncestor, Self, TemplatedParent, and implicit mode inference. Commit bb58114.

Copilot AI requested a review from simonrozsival November 30, 2025 22:44
@simonrozsival
simonrozsival marked this pull request as ready for review November 30, 2025 23:17
Copilot AI review requested due to automatic review settings November 30, 2025 23:17
@simonrozsival

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes RelativeSource bindings with FindAncestorBindingContext mode when the XAML source generator (XSG) is enabled. The issue was that RelativeSourceExtension was not registered in the source generator's known markup extensions, causing bindings to fail at runtime.

Key changes:

  • Adds source generator support for RelativeSourceExtension with proper code generation for all modes
  • Implements automatic mode inference (Element types → FindAncestor, non-Element types → FindAncestorBindingContext)
  • Adds comprehensive test coverage for all RelativeSource modes across all inflators

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Controls/src/SourceGen/KnownMarkups.cs Implements ProvideValueForRelativeSourceExtension to generate correct RelativeBindingSource instantiation code for all modes with proper mode inference
src/Controls/src/SourceGen/NodeSGExtensions.cs Registers RelativeSourceExtension in the known early markup extensions dictionary
src/Controls/tests/Xaml.UnitTests/Issues/Maui32924.xaml Adds XAML test cases for explicit/implicit FindAncestorBindingContext, FindAncestor, and Self modes
src/Controls/tests/Xaml.UnitTests/Issues/Maui32924.xaml.cs Adds unit tests validating all RelativeSource modes work across all inflators (Runtime, XamlC, SourceGen)
src/Controls/tests/SourceGen.UnitTests/InitializeComponent/RelativeSourceBindings.cs Adds source generator unit tests verifying generated code doesn't use XamlTypeResolver (preventing trimming/AOT warnings)

@simonrozsival

Copy link
Copy Markdown
Member

I went through the failing UI tests and I'm confident they are all unrelated.

@simonrozsival simonrozsival added the area-xaml XAML, CSS, Triggers, Behaviors label Dec 1, 2025
@StephaneDelcroix
StephaneDelcroix merged commit 64d777d into main Dec 1, 2025
@StephaneDelcroix
StephaneDelcroix deleted the copilot/fix-relative-source-binding branch December 1, 2025 08:34
PureWeen pushed a commit that referenced this pull request Dec 30, 2025
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description

When a binding has a `Source` property with a `RelativeSourceExtension`,
skip the compiled binding path and use the fallback string-based binding
instead.

### Root Cause

When PR #32925 added RelativeSource support to SourceGen, it also
inadvertently caused bindings with `RelativeSource` to be compiled using
`x:DataType` as the source type. This is incorrect because the source
type for `RelativeSource` bindings is determined at runtime, not at
compile time.

For example, with:
```xml
<ContentView x:DataType="local:ChildViewModel">
    <TapGestureRecognizer CommandParameter="{Binding Path=., Source={RelativeSource AncestorType={x:Type local:MainPage}}}" />
</ContentView>
```

The SourceGen was generating:
```csharp
return new TypedBinding<ChildViewModel, ChildViewModel>(
    getter: source => (source, true),  // Returns ChildViewModel, not MainPage!
    ...
);
```

### Fix

Added a check in `ProvideValueForBindingExtension` to detect when the
binding has a `Source` property with a `RelativeSourceExtension`. In
this case, we skip the compiled binding path and use the fallback
string-based binding instead, which correctly resolves the source at
runtime.

### Issues Fixed
Fixes #33247

### Testing

- Added SourceGen unit tests to verify correct code generation
- Added XAML unit tests to verify the binding works correctly at runtime
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 31, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-xaml XAML, CSS, Triggers, Behaviors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[XSG] RelativeSource binding with FindAncestorBindingContext does not work

4 participants