Skip to content

MAUIG2045 false positive when binding to parent BindingContext via x:Reference inside DataTemplate #34490

@gmwilhelm

Description

@gmwilhelm

Description

When using Source={x:Reference} with Path=BindingContext.PropertyName inside a DataTemplate, the XAML source generator emits MAUIG2045 warnings because it tries to
resolve BindingContext as a property on the DataTemplate's x:DataType instead of on the referenced element.

This is a common pattern to bind to a parent ViewModel's commands/properties from within a DataTemplate that has its own x:DataType set to a model type.

Steps to Reproduce

  1. Create a ContentPage with a CollectionView using a DataTemplate
  2. Set x:DataType on the DataTemplate to a model type
  3. Inside the DataTemplate, bind to the parent page's ViewModel via Source={x:Reference}

  <CollectionView ItemsSource="{Binding Items}">
      <CollectionView.ItemTemplate>
          <DataTemplate x:DataType="models:ItemModel">
              <!-- This binding works at runtime but emits MAUIG2045 -->
              <Button Text="{Binding Name}"
                      Command="{Binding Source={x:Reference PageMyPage},
                                       Path=BindingContext.SelectItemCommand}"
                      CommandParameter="{Binding .}" />
          </DataTemplate>
      </CollectionView.ItemTemplate>
  </CollectionView>

Expected Behavior

No warning, since the Source is explicitly set to an x:Reference of the page (a BindableObject which has BindingContext). The source generator should resolve
BindingContext on the referenced element's type, not on the DataTemplate's x:DataType.

Actual Behavior

MAUIG2045: Binding: Property "BindingContext" not found on "global::MyApp.Models.ItemModel".
The binding will use slower reflection-based binding at runtime.

The source generator ignores the Source and resolves the Path against the current x:DataType scope (ItemModel), which obviously doesn't have a BindingContext
property.

Link to public reproduction project repository

No response

Version with bug

10.0.50

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

10.0.30

Affected platforms

iOS, Android

Affected platform versions

No response

Did you find any workaround?

Suppress the warning:
$(NoWarn);MAUIG2045

Relevant log output

MAUIG2045: Binding: Property "BindingContext" not found on "global::MyApp.Models.ItemModel". The binding will use slower reflection-based binding at runtime.

Metadata

Metadata

Labels

area-xamlXAML, CSS, Triggers, Behaviorsi/regressionThis issue described a confirmed regression on a currently supported versionregressed-in-10.0.40s/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't workingxsgXaml sourceGen

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions