Search code examples
xamlmauicompiled-bindings

MAUI: Binding to my viewmodel throws an error before compilation (prop not found)


I have a simple view model:

public List<DisplayModel> Items { get; set; } = new List<DisplayModel>();

public class DisplayModel
{
    public string Name { get; set; }
    public string Prop { get; set; }   
}

In my view, I have this collection view:

<CollectionView ItemsSource="{Binding Items}" >
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <VerticalStackLayout>
                <Grid ColumnSpacing="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Label TextColor="White" FontSize="18" FontFamily="WuerthExtraBold"  Text="{Binding Name}"/>
                    <Label TextColor="White" FontSize="18" FontAttributes="Bold"   Grid.Column="1" HorizontalTextAlignment="End" HorizontalOptions="End" Text="{Binding Prop}"/>
                </Grid>
            </VerticalStackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

but as soon as I hit compile, it tell me that the property name or prop arent found:

enter image description here

I can bind to that list, but just not with those properties. What is going on?


Solution

  • you need to add a DataType

    <DataTemplate x:DataType="local:DisplayModel">