[XSG] Fix SourceGen typed binding setter for ObservableProperty#33914
Merged
[XSG] Fix SourceGen typed binding setter for ObservableProperty#33914
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in SourceGen where typed bindings for properties generated by CommunityToolkit.Mvvm's [ObservableProperty] attribute were not generating setters, causing TwoWay bindings to silently fail.
Changes:
- Added
isObservablePropertyout parameter toTryGetPropertymethod to track when a property is inferred from an ObservableProperty attribute - Modified setter generation logic to treat ObservableProperty-generated properties as writable (they always have public setters)
- Added comprehensive unit tests verifying both directions of TwoWay bindings with ObservableProperty
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Controls/src/SourceGen/ITypeSymbolExtensions.cs |
Added method overload with isObservableProperty out parameter; maintains backward compatibility |
src/Controls/src/SourceGen/CompiledBindingMarkup.cs |
Updated to use new isObservableProperty parameter and treat such properties as writable |
src/Controls/tests/Xaml.UnitTests/Issues/Maui33826.xaml |
XAML test file with CollectionView using TwoWay bindings to ObservableProperty |
src/Controls/tests/Xaml.UnitTests/Issues/Maui33826.xaml.cs |
Test code with ViewModel using [ObservableProperty] and comprehensive binding tests |
simonrozsival
approved these changes
Feb 6, 2026
When binding to a property generated by CommunityToolkit.Mvvm's [ObservableProperty] attribute, the SourceGen typed binding was not generating a setter. This caused TwoWay bindings to fail silently - the SelectedItem would not update the ViewModel property. The fix: 1. Added 'isObservableProperty' out parameter to TryGetProperty 2. When an ObservableProperty is detected, assume it's writable (ObservableProperty-generated properties always have a public setter) This fixes the crash in template apps using SourceGen with CommunityToolkit.Mvvm when tapping on CollectionView items bound to SelectionChangedCommand with a parameter bound to the SelectedItem. Added unit test Maui33826 to verify the fix.
Address review feedback from @simonrozsival - use more semantic variable name that better describes the intent of the flag.
22e9d29 to
09eb5a2
Compare
simonrozsival
approved these changes
Feb 9, 2026
Member
|
/azp run maui-pr-uitests, maui-pr-devicetests |
|
Azure Pipelines successfully started running 2 pipeline(s). |
This was referenced Feb 9, 2026
Contributor
Author
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
TamilarasanSF4853
pushed a commit
to TamilarasanSF4853/maui
that referenced
this pull request
Mar 2, 2026
…et#33914) > [!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 Fixes a bug in SourceGen where typed bindings for properties generated by CommunityToolkit.Mvvm's `[ObservableProperty]` attribute were not generating setters. ## Root Cause When `TryGetProperty` in `ITypeSymbolExtensions.cs` detects an `[ObservableProperty]` field, it returns the property type but leaves the `property` output parameter as null (since the property doesn't exist in source - it's generated by CommunityToolkit). In `CompiledBindingMarkup.cs`, `IsWritable` was checking `property.SetMethod`, which fails when `property` is null, causing `setter = null` to be generated for TwoWay bindings. ## The Fix - Added `isAssumedWritable` out parameter to `TryGetProperty` - When ObservableProperty fallback is used, set `isAssumedWritable = true` - In `CompiledBindingMarkup.cs`, treat `isAssumedWritable` properties as writable (CommunityToolkit always generates public setters) ## Reproduction The issue can be reproduced with a ViewModel like: ```csharp public partial class MyViewModel : ObservableObject { [ObservableProperty] private Project? _selectedProject; } ``` And XAML binding: ```xml <CollectionView SelectedItem="{Binding SelectedProject}" /> ``` With SourceGen, the setter was null, causing TwoWay bindings to silently fail. ## Testing - Added unit test `Maui33826` that verifies the setter is generated correctly - Verified with real app using Appium automation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 a bug in SourceGen where typed bindings for properties generated by CommunityToolkit.Mvvm's
[ObservableProperty]attribute were not generating setters.Root Cause
When
TryGetPropertyinITypeSymbolExtensions.csdetects an[ObservableProperty]field, it returns the property type but leaves thepropertyoutput parameter as null (since the property doesn't exist in source - it's generated by CommunityToolkit).In
CompiledBindingMarkup.cs,IsWritablewas checkingproperty.SetMethod, which fails whenpropertyis null, causingsetter = nullto be generated for TwoWay bindings.The Fix
isAssumedWritableout parameter toTryGetPropertyisAssumedWritable = trueCompiledBindingMarkup.cs, treatisAssumedWritableproperties as writable (CommunityToolkit always generates public setters)Reproduction
The issue can be reproduced with a ViewModel like:
And XAML binding:
With SourceGen, the setter was null, causing TwoWay bindings to silently fail.
Testing
Maui33826that verifies the setter is generated correctly