Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 6, 2025

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 of Change

Binding source generators now support properties generated by CommunityToolkit.Mvvm's [ObservableProperty] attribute. Previously, bindings failed because MAUI generators cannot see properties generated by other source generators.

Implementation (following PR #32954 pattern for RelayCommand):

  • BindingSourceGen/ITypeSymbolExtensions.cs: Added TryGetObservablePropertyType() to detect fields with [ObservableProperty] and infer property types. Supports both camelCase and _camelCase field naming patterns.

  • BindingSourceGen/PathParser.cs: Enhanced TryHandleSpecialCases() to handle ObservableProperty patterns as fallback when symbol resolution fails.

  • BindingSourceGen/BindingSourceGenerator.cs: Updated GetLambdaReturnType() to infer types from ObservableProperty fields, enabling C# lambda bindings.

  • SourceGen/ITypeSymbolExtensions.cs: Updated TryGetProperty() to check ObservableProperty-inferred properties, enabling XAML string-based bindings.

  • Tests: Added comprehensive unit tests in both BindingSourceGen.UnitTests (7 tests) and Xaml.UnitTests (3 tests) to validate ObservableProperty binding support across all scenarios and XamlInflator modes.

Works for both XAML and C# bindings:

public class MyViewModel
{
    [ObservableProperty]
    private string? name;  // Generates Name property
    
    [ObservableProperty]
    private ObservableCollection<Tag> _tags = new();  // Generates Tags property
}
<Label Text="{Binding Name}" />
<ListView ItemsSource="{Binding Tags}" />
label.SetBinding(Label.TextProperty, static (MyViewModel vm) => vm.Name);
listView.SetBinding(ListView.ItemsSourceProperty, static (MyViewModel vm) => vm.Tags);

Issues Fixed

Fixes #32597

Original prompt

This section details on the original issue you should resolve

<issue_title>[XSG][BindingSourceGen] Add support for CommunityToolkit.Mvvm ObservablePropertyAttribute source generator</issue_title>
<issue_description>Similar to #32597
Related to #32953

The CommunityToolkit.Mvvm package contains a source generator which generates bindable property bolierplate code based on the [ObservableProperty] attribute: https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/observableproperty

Scenarios:

// applied to a field
[ObservableProperty]
private string? name;

// generated code:
/// <inheritdoc cref="_tags"/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.3.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public global::System.Collections.ObjectModel.ObservableCollection<global::TestSampleContentBindings.Models.Tag> Tags
{
    get => _tags;
    [global::System.Diagnostics.CodeAnalysis.MemberNotNull("_tags")]
    set
    {
        if (!global::System.Collections.Generic.EqualityComparer<global::System.Collections.ObjectModel.ObservableCollection<global::TestSampleContentBindings.Models.Tag>>.Default.Equals(_tags, value))
        {
            OnTagsChanging(value);
            OnTagsChanging(default, value);
            OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Tags);
            _tags = value;
            OnTagsChanged(value);
            OnTagsChanged(default, value);
            OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Tags);
        }
    }
}

Our source generators cannot see properties generated by other source generators, so the binding compilation will fail.

Our sample content template uses this source generator and we are currently failing over to reflection-based bindings.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add support for CommunityToolkit.Mvvm ObservablePropertyAttribute generator [XSG][BindingSourceGen] Add support for CommunityToolkit.Mvvm ObservablePropertyAttribute Dec 6, 2025
Copilot AI requested a review from simonrozsival December 6, 2025 23:18
Copilot finished work on behalf of simonrozsival December 6, 2025 23:18
@simonrozsival
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

@simonrozsival simonrozsival force-pushed the copilot/add-observable-property-support branch from 6c9d40b to c039c1f Compare December 8, 2025 17:44
@simonrozsival
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

Copy link
Member

@simonrozsival simonrozsival left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please add also a test case in Xaml.UnitTest

Copy link
Contributor Author

Copilot AI commented Dec 8, 2025

@copilot please add also a test case in Xaml.UnitTest

Added XAML unit tests in commit 5a2eadd. The tests cover bindings to ObservableProperty-generated properties from camelCase fields (nameName), underscore-prefixed fields (_titleTitle), and collections (_tagsTags). Tests validate all three XamlInflator modes (Runtime, XamlC, SourceGen).

Copilot AI requested a review from simonrozsival December 8, 2025 18:55
Copilot finished work on behalf of simonrozsival December 8, 2025 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants