-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
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
- Create a ContentPage with a CollectionView using a DataTemplate
- Set x:DataType on the DataTemplate to a model type
- 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.