Skip to content

[XamlC] Produce warning when Binding inside of DataTemplate inherits x:DataType from outside of the template #22714

@simonrozsival

Description

@simonrozsival

I think XamlC should produce a warning when there is a {Binding ...} inside of a DataTemplate which inherits x:DataType from outside of the template. Consider the following example:

<ScrollView x:DataType="local:PageViewModel">
    <StackLayout BindableLayout.ItemsSource="{Binding Items}">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <Label Text="{Binding .}" />
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>
</ScrollView>

My suggestion is to report a warning something like

XC0024: Binding might be compiled incorrectly because it is using an x:DataType local:PageViewModel that is defined outside of the current DataTemplate scope. Consider adding x:DataType directly to the DataTemplate. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information.

... and add a paragraph describing this issue to the linked documentation page.

Related to #22056

/cc @StephaneDelcroix @ivanpovazan @PureWeen @mattleibow

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions