Binding property "fromICAO" not found on "ViewModel" in collectionview

Wout Ongena 276 Reputation points
2021-11-02T16:16:48.63+00:00

I am trying to show data using a collection view in Xamarin but the Binding for the text in one of the labels cannot find the property in the viewmodel

my view:

<CollectionView x:Name="ItemsCollectionView"
                                ItemsSource="{Binding Plans}"
                                SelectionMode="None">
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <Frame HasShadow="True">
                                <StackLayout>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="60"/>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition/>
                                            <ColumnDefinition/>
                                        </Grid.ColumnDefinitions>
                                        <StackLayout Spacing="0">
                                            <Label Text="From:" 
                                                   FontSize="10"
                                                   TextColor="Black"></Label>
                                            <Label Text="{Binding fromICAO}" 
                                                   FontSize="24"
                                                   TextColor="Black"></Label>
                                            <Label Text="To:" 
                                                   FontSize="10"
                                                   TextColor="Black"></Label>
                                            <Label Text="{Binding toICAO}" 
                                                   FontSize="24"
                                                   TextColor="Black"></Label>
                                        </StackLayout>
                                        <StackLayout  Spacing="0"
                                                      Grid.Column="1">
                                            <Label Text="Distance:" 
                                                   FontSize="10"
                                                   TextColor="Black"></Label>
                                            <Label Text="{Binding distance}" 
                                                   FontSize="24"
                                                   TextColor="Black"></Label>
                                            <Label Text="Fuel used:" 
                                                   FontSize="10"
                                                   TextColor="Black"></Label>
                                            <Label Text="{Binding fuelUsed}" 
                                                   FontSize="24"
                                                   TextColor="Black"></Label>
                                        </StackLayout>
                                    </Grid>
                                </StackLayout>
                            </Frame>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>

my viewmodel:

public ObservableCollection<PlanToFirestoreModel> Plans { get; set; }

        public PlansViewModel() {
            firestoreService = DependencyService.Get<IFirestoreService>();
            auth = DependencyService.Get<IFirebaseAuthentication>();
            Plans = new ObservableCollection<PlanToFirestoreModel>();

            FillData();
        }

        public async void FillData()
        {
            var tempPlans = await firestoreService.GetFlightPlansByUid(auth.GetUserInfoAsync().Id);
            foreach (var plan in tempPlans)
            {
                Plans.Add(plan);
            }
        }

and my Model itself:

public class PlanToFirestoreModel : BasePlanModel
    {
        public string fromICAO { get; set; }
        public string toICAO { get; set; }
        public string fromName { get; set; }
        public string toName { get; set; }

        public int id { get; set; }
        public string userId { get; set; }
        public string aircraft { get; set; }
        public string deptCountry { get; set; }
        public string arrCountry { get; set; }
        public double fuelUsed { get; set; }
        public string flightNumber { get; set; }
        public double distance { get; set; }
        public string altitude { get; set; }
        public string createdAt { get; set; }
        public double deptLon { get; set; }
        public double deptLat { get; set; }
        public double arrLon { get; set; }
        public double arrLat { get; set; }
    }

The XAML page can find the Plans ObservableCollection itself but not the properties that are supposed to be in the list like "fromICAO" and "toICAO"
What am I doing wrong?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Wout Ongena 276 Reputation points
    2021-11-04T10:14:03.453+00:00

    By changing the way I bound my viewmodel from:

     xmlns:viewmodels="clr-namespace:FlightBox.ViewModels" x:DataType="viewmodels:PlansViewModel"
    

    To:

    xmlns:viewmodel="clr-namespace:FlightBox.ViewModels"
    
    <d:ContentPage.BindingContext>
        <viewmodel:PlansViewModel />
    </d:ContentPage.BindingContext>
    

    It worked.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,196 Reputation points
    2021-11-02T16:22:26.027+00:00

    fromICAO is not present in PlanToFirestoreModel. For example, fuelUsed is present...


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.