-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[XSG] Fix RelativeSource AncestorType x:Type resolution #33882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Improve ProvideValueForRelativeSourceExtension to resolve x:Type inline when it's not yet cached in context.Types - Check all namespace variations for AncestorType property (empty, null, MauiUri) - Fix SwipeViewBindingContextGallery sample to use correct namespace (was using Microsoft.Maui.Controls.ControlGallery... instead of Maui.Controls.Sample...) - Remove [XamlCompilation(Skip)] from SwipeViewBindingContextGallery since it now compiles - Add unit test for RelativeSource inside DataTemplate Fixes #33876
There was a problem hiding this comment.
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 XSG (XAML Source Generator) handling of RelativeSource bindings with AncestorType={x:Type ...} syntax. Previously, XSG would fail with error "Invalid RelativeSource Mode '(none)'" when encountering this pattern inside DataTemplates.
Changes:
- Improved x:Type resolution in XSG to handle cases where x:Type extensions are not yet cached
- Fixed incorrect namespace in SwipeViewBindingContextGallery.xaml sample
- Added test coverage for RelativeSource inside DataTemplate scenarios
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Controls/src/SourceGen/KnownMarkups.cs | Enhanced ProvideValueForRelativeSourceExtension to resolve x:Type inline when not cached, and checks multiple namespace variations for AncestorType property |
| src/Controls/tests/Xaml.UnitTests/Issues/Maui33876.xaml | Test XAML demonstrating RelativeSource with AncestorType inside a DataTemplate |
| src/Controls/tests/Xaml.UnitTests/Issues/Maui33876.xaml.cs | Test code-behind with test case for the RelativeSource fix |
| src/Controls/samples/Controls.Sample/Pages/Controls/SwipeViewGalleries/SwipeViewBindingContextGallery.xaml | Fixed incorrect namespace from Microsoft.Maui.Controls.ControlGallery.GalleryPages.SwipeViewGalleries to correct Maui.Controls.Sample.Pages.SwipeViewGalleries |
| src/Controls/samples/Controls.Sample/Pages/Controls/SwipeViewGalleries/SwipeViewBindingContextGallery.xaml.cs | Removed [XamlCompilation(XamlCompilationOptions.Skip)] and unused using statement since file now compiles correctly |
| { | ||
| public Maui33876() => InitializeComponent(); | ||
|
|
||
| [Collection("Issue")] |
Copilot
AI
Feb 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the XAML Unit Testing Guidelines, test classes should use [Collection("Issue")] instead of [TestFixture]. [TestFixture] is a NUnit attribute, but this codebase uses xUnit for XAML unit tests. All other test files in the Issues directory consistently use [Collection("Issue")].
| [Theory] | ||
| [XamlInflatorData] |
Copilot
AI
Feb 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the XAML Unit Testing Guidelines, test methods should use [Theory] with [XamlInflatorData] instead of [Test] with [Values] XamlInflator inflator. The correct pattern is used throughout the codebase, as seen in other recent tests like Maui32697.xaml.cs and Maui33676.xaml.cs.
| { | ||
| // x:Type extension not yet processed - resolve it now | ||
| // This can happen when RelativeSource is processed before the x:Type child | ||
| if (!typeExtNode.Properties.TryGetValue(new XmlName("", "TypeName"), out INode? typeNameNode) |
Copilot
AI
Feb 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TypeName property check is missing a null namespace variant. Other property lookups in this method (Mode at lines 138-139, AncestorType at lines 152-153, AncestorLevel at lines 202-203) check for both empty string and null namespace variants. The new code should check for new XmlName(null, "TypeName") as well to be consistent with the rest of the codebase and handle all possible namespace variations.
| if (!typeExtNode.Properties.TryGetValue(new XmlName("", "TypeName"), out INode? typeNameNode) | |
| if (!typeExtNode.Properties.TryGetValue(new XmlName("", "TypeName"), out INode? typeNameNode) | |
| && !typeExtNode.Properties.TryGetValue(new XmlName(null, "TypeName"), out typeNameNode) |
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
Fixes XSG (XAML Source Generator) handling of
RelativeSourcewithAncestorType={x:Type ...}.Issue:
When using
{RelativeSource AncestorType={x:Type vm:Foo}}inside a DataTemplate, XSG would fail with:Root causes:
SwipeViewBindingContextGallery.xamlhad an incorrectclr-namespace(Microsoft.Maui.Controls.ControlGallery...instead ofMaui.Controls.Sample...), which caused the x:Type to fail resolutionProvideValueForRelativeSourceExtensionwasn't checking all namespace variations for the AncestorType propertyChanges
KnownMarkups.cs:
ProvideValueForRelativeSourceExtensionto resolve inline when not cachedSwipeViewBindingContextGallery.xaml:
xmlns:vmto use correct namespace (Maui.Controls.Sample.Pages.SwipeViewGalleries)SwipeViewBindingContextGallery.xaml.cs:
[XamlCompilation(XamlCompilationOptions.Skip)]since file now compiles correctlyTests:
Maui33876.xamltest for RelativeSource inside DataTemplateFixes #33876