diff --git a/src/Controls/src/SourceGen/CompiledBindingMarkup.cs b/src/Controls/src/SourceGen/CompiledBindingMarkup.cs
index a7e8bffd4c21..d943386940a9 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 isAssumedWritable)
|| 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 = isAssumedWritable
+ || (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..ed3a786f3f56 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 isAssumedWritable)
+ {
+ isAssumedWritable = 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))
{
+ isAssumedWritable = 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);
+ }
+ }
+}