From 3cc3b5c6489b25ddacc52f7241b78ce82396093f Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Thu, 5 Feb 2026 14:13:41 +0100 Subject: [PATCH 1/2] Fix SourceGen typed binding setter for ObservableProperty 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. --- .../src/SourceGen/CompiledBindingMarkup.cs | 17 ++- .../src/SourceGen/ITypeSymbolExtensions.cs | 24 ++++ .../Xaml.UnitTests/Issues/Maui33826.xaml | 25 ++++ .../Xaml.UnitTests/Issues/Maui33826.xaml.cs | 107 ++++++++++++++++++ 4 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 src/Controls/tests/Xaml.UnitTests/Issues/Maui33826.xaml create mode 100644 src/Controls/tests/Xaml.UnitTests/Issues/Maui33826.xaml.cs diff --git a/src/Controls/src/SourceGen/CompiledBindingMarkup.cs b/src/Controls/src/SourceGen/CompiledBindingMarkup.cs index a7e8bffd4c21..392c61d5123b 100644 --- a/src/Controls/src/SourceGen/CompiledBindingMarkup.cs +++ b/src/Controls/src/SourceGen/CompiledBindingMarkup.cs @@ -306,8 +306,8 @@ bool TryParsePath( if (p.Length > 0) { - // Try to find property or infer from RelayCommand method - if (!previousPartType.TryGetProperty(p, _context, out var property, out var currentPropertyType) + // Try to find property or infer from RelayCommand method or ObservableProperty attribute + if (!previousPartType.TryGetProperty(p, _context, out var property, out var currentPropertyType, out var isObservableProperty) || currentPropertyType is null) { // Report as WARNING (not Error) because the property might be generated by another source generator @@ -335,13 +335,18 @@ bool TryParsePath( bindingPathParts.Add(memberAccess); - // TODO: do this only if it is the last part? - setterOptions = new SetterOptions( - IsWritable: property is not null + // Determine if the property is writable + // ObservableProperty-generated properties are always writable (they have a public setter) + bool isWritable = isObservableProperty + || (property is not null && property.SetMethod is not null && property.SetMethod.IsPublic() && !property.SetMethod.IsInitOnly - && !property.SetMethod.IsStatic, + && !property.SetMethod.IsStatic); + + // TODO: do this only if it is the last part? + setterOptions = new SetterOptions( + IsWritable: isWritable, AcceptsNullValue: memberIsNullable); previousPartType = currentPropertyType; diff --git a/src/Controls/src/SourceGen/ITypeSymbolExtensions.cs b/src/Controls/src/SourceGen/ITypeSymbolExtensions.cs index 6067880c34ad..9ae86a6e22d2 100644 --- a/src/Controls/src/SourceGen/ITypeSymbolExtensions.cs +++ b/src/Controls/src/SourceGen/ITypeSymbolExtensions.cs @@ -100,6 +100,29 @@ public static bool TryGetProperty( out IPropertySymbol? property, out ITypeSymbol? propertyType) { + return TryGetProperty(symbol, propertyName, context, out property, out propertyType, out _); + } + + /// + /// Tries to get a property by name, and if not found, checks if it could be inferred from a RelayCommand method + /// or an ObservableProperty attribute. Returns the property type if found or inferred. + /// + /// The type to search + /// The name of the property to find + /// The source generation context + /// The found property symbol (null if inferred from RelayCommand/ObservableProperty) + /// The property type (either from the property or inferred) + /// True if the property was inferred from an ObservableProperty attribute (which is always writable) + /// True if property exists or can be inferred + public static bool TryGetProperty( + this ITypeSymbol symbol, + string propertyName, + SourceGenContext? context, + out IPropertySymbol? property, + out ITypeSymbol? propertyType, + out bool isObservableProperty) + { + isObservableProperty = false; property = symbol.GetAllProperties(propertyName, context) .FirstOrDefault(p => p.GetMethod != null && !p.GetMethod.IsStatic); @@ -120,6 +143,7 @@ public static bool TryGetProperty( // Call the BindingSourceGen extension method directly if (symbol.TryGetObservablePropertyType(propertyName, context?.Compilation, out propertyType)) { + isObservableProperty = true; return true; } diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui33826.xaml b/src/Controls/tests/Xaml.UnitTests/Issues/Maui33826.xaml new file mode 100644 index 000000000000..ece1248157c3 --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui33826.xaml @@ -0,0 +1,25 @@ + + + + + + + + + + diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui33826.xaml.cs b/src/Controls/tests/Xaml.UnitTests/Issues/Maui33826.xaml.cs new file mode 100644 index 000000000000..cc1ee767904a --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui33826.xaml.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using CommunityToolkit.Mvvm.ComponentModel; +using Microsoft.Maui.Controls.Internals; +using Microsoft.Maui.Dispatching; +using Microsoft.Maui.UnitTests; +using Xunit; + +#nullable enable + +namespace Microsoft.Maui.Controls.Xaml.UnitTests; + +/// +/// Test model for Maui33826 +/// +public class Maui33826Project +{ + public int Id { get; set; } + public string Name { get; set; } = string.Empty; +} + +/// +/// ViewModel using CommunityToolkit.Mvvm's [ObservableProperty] attribute. +/// The SelectedProject property is generated by the CommunityToolkit source generator. +/// MAUI's SourceGen should detect this and generate a setter for typed bindings. +/// +public partial class Maui33826ViewModel : ObservableObject +{ + [ObservableProperty] + private List _projects = []; + + [ObservableProperty] + private Maui33826Project? _selectedProject; +} + +public partial class Maui33826 : ContentPage +{ + public Maui33826() + { + InitializeComponent(); + } + + [Collection("Xaml Inflation")] + public class Tests : IDisposable + { + public Tests() => DispatcherProvider.SetCurrent(new DispatcherProviderStub()); + public void Dispose() => DispatcherProvider.SetCurrent(null); + + [Theory] + [XamlInflatorData] + internal void SelectedItemBindingHasSetter(XamlInflator inflator) + { + // Arrange + var project1 = new Maui33826Project { Id = 1, Name = "Project 1" }; + var project2 = new Maui33826Project { Id = 2, Name = "Project 2" }; + + var vm = new Maui33826ViewModel + { + Projects = [project1, project2] + }; + + var page = new Maui33826(inflator) + { + BindingContext = vm + }; + + // Act - Simulate selecting an item in the CollectionView + // This should update vm.SelectedProject via the TwoWay binding + page.collectionView.SelectedItem = project1; + + // Assert - The binding should have updated the ViewModel property + // If SourceGen doesn't generate a setter, SelectedProject will remain null + Assert.NotNull(vm.SelectedProject); + Assert.Equal(project1.Id, vm.SelectedProject.Id); + Assert.Equal("Project 1", vm.SelectedProject.Name); + } + + [Theory] + [XamlInflatorData] + internal void SelectedItemBindingUpdatesFromViewModel(XamlInflator inflator) + { + // Arrange + var project1 = new Maui33826Project { Id = 1, Name = "Project 1" }; + var project2 = new Maui33826Project { Id = 2, Name = "Project 2" }; + + var vm = new Maui33826ViewModel + { + Projects = [project1, project2], + SelectedProject = project1 + }; + + var page = new Maui33826(inflator) + { + BindingContext = vm + }; + + // Assert - The CollectionView should reflect the ViewModel's SelectedProject + Assert.Equal(project1, page.collectionView.SelectedItem); + + // Act - Update ViewModel + vm.SelectedProject = project2; + + // Assert - CollectionView should update (OneWay direction - getter works) + Assert.Equal(project2, page.collectionView.SelectedItem); + } + } +} From 09eb5a2b885a1ed578cc2d004c3e279999bb834c Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Fri, 6 Feb 2026 08:25:22 +0000 Subject: [PATCH 2/2] Rename isObservableProperty to isAssumedWritable Address review feedback from @simonrozsival - use more semantic variable name that better describes the intent of the flag. --- src/Controls/src/SourceGen/CompiledBindingMarkup.cs | 4 ++-- src/Controls/src/SourceGen/ITypeSymbolExtensions.cs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Controls/src/SourceGen/CompiledBindingMarkup.cs b/src/Controls/src/SourceGen/CompiledBindingMarkup.cs index 392c61d5123b..d943386940a9 100644 --- a/src/Controls/src/SourceGen/CompiledBindingMarkup.cs +++ b/src/Controls/src/SourceGen/CompiledBindingMarkup.cs @@ -307,7 +307,7 @@ bool TryParsePath( if (p.Length > 0) { // Try to find property or infer from RelayCommand method or ObservableProperty attribute - if (!previousPartType.TryGetProperty(p, _context, out var property, out var currentPropertyType, out var isObservableProperty) + if (!previousPartType.TryGetProperty(p, _context, out var property, out var currentPropertyType, out var isAssumedWritable) || currentPropertyType is null) { // Report as WARNING (not Error) because the property might be generated by another source generator @@ -337,7 +337,7 @@ bool TryParsePath( // Determine if the property is writable // ObservableProperty-generated properties are always writable (they have a public setter) - bool isWritable = isObservableProperty + bool isWritable = isAssumedWritable || (property is not null && property.SetMethod is not null && property.SetMethod.IsPublic() diff --git a/src/Controls/src/SourceGen/ITypeSymbolExtensions.cs b/src/Controls/src/SourceGen/ITypeSymbolExtensions.cs index 9ae86a6e22d2..ed3a786f3f56 100644 --- a/src/Controls/src/SourceGen/ITypeSymbolExtensions.cs +++ b/src/Controls/src/SourceGen/ITypeSymbolExtensions.cs @@ -112,7 +112,7 @@ public static bool TryGetProperty( /// The source generation context /// The found property symbol (null if inferred from RelayCommand/ObservableProperty) /// The property type (either from the property or inferred) - /// True if the property was inferred from an ObservableProperty attribute (which is always writable) + /// True if the property was inferred from an ObservableProperty attribute (which is always writable) /// True if property exists or can be inferred public static bool TryGetProperty( this ITypeSymbol symbol, @@ -120,9 +120,9 @@ public static bool TryGetProperty( SourceGenContext? context, out IPropertySymbol? property, out ITypeSymbol? propertyType, - out bool isObservableProperty) + out bool isAssumedWritable) { - isObservableProperty = false; + isAssumedWritable = false; property = symbol.GetAllProperties(propertyName, context) .FirstOrDefault(p => p.GetMethod != null && !p.GetMethod.IsStatic); @@ -143,7 +143,7 @@ public static bool TryGetProperty( // Call the BindingSourceGen extension method directly if (symbol.TryGetObservablePropertyType(propertyName, context?.Compilation, out propertyType)) { - isObservableProperty = true; + isAssumedWritable = true; return true; }