Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Controls/src/SourceGen/CompiledBindingMarkup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions src/Controls/src/SourceGen/ITypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ public static bool TryGetProperty(
out IPropertySymbol? property,
out ITypeSymbol? propertyType)
{
return TryGetProperty(symbol, propertyName, context, out property, out propertyType, out _);
}

/// <summary>
/// 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.
/// </summary>
/// <param name="symbol">The type to search</param>
/// <param name="propertyName">The name of the property to find</param>
/// <param name="context">The source generation context</param>
/// <param name="property">The found property symbol (null if inferred from RelayCommand/ObservableProperty)</param>
/// <param name="propertyType">The property type (either from the property or inferred)</param>
/// <param name="isAssumedWritable">True if the property was inferred from an ObservableProperty attribute (which is always writable)</param>
/// <returns>True if property exists or can be inferred</returns>
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);

Expand All @@ -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;
}

Expand Down
25 changes: 25 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui33826.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui33826"
x:DataType="local:Maui33826ViewModel">
<!--
Test for GitHub issue #33826
SourceGen typed bindings should generate setters for properties
created by CommunityToolkit.Mvvm's [ObservableProperty] attribute.

The SelectedItem binding needs TwoWay support to update SelectedProject
when the user selects an item in the CollectionView.
-->
<CollectionView x:Name="collectionView"
ItemsSource="{Binding Projects}"
SelectedItem="{Binding SelectedProject}"
SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="local:Maui33826Project">
<Label Text="{Binding Name}" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage>
107 changes: 107 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui33826.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Test model for Maui33826
/// </summary>
public class Maui33826Project
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}

/// <summary>
/// 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.
/// </summary>
public partial class Maui33826ViewModel : ObservableObject
{
[ObservableProperty]
private List<Maui33826Project> _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);
}
}
}
Loading