Search code examples
xamlxamarin.formsmvvmdata-binding

Xamarin Data Binding - issue showing data from custom class via ItemList


I have the following class defined in the model:

public class Equipment
{

    public Equipment(string resName, DateTime crTime, int quant)
    {
        ResourceName = resName;
        CreateTime = crTime;
        Quantity = quant;
    }

    public string ResourceName { get; set; }

    public DateTime CreateTime { get; set;  }

    public int Quantity { get; set; }


}

I use a viewModel where I define it as

class EquipementViewModel : ObservableObject
   {
       ObservableCollection<Equipment> equipRange = new ObservableCollection<Equipment>();
       public ObservableCollection<Equipment> EquipRange { get { return equipRange; }  }
       public EquipementViewModel()
         {
                     
           equipRange.Add(new Equipment("Komax01",DateTime.Now,1));
           equipRange.Add(new Equipment("Komax02", DateTime.Now, 1));
           equipRange.Add(new Equipment("Komax03", DateTime.Now, 1));
       }
}

And then finally the View XAML

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:viewmodels="clr-namespace:TestXamarin.ViewModels"
             x:Class="TestXamarin.Views.EquipementPage"
              Title="EQuip"
             x:DataType="viewmodels:EquipementViewModel">
    <ContentPage.BindingContext>
        <viewmodels:EquipementViewModel/>
    </ContentPage.BindingContext>
    <ListView ItemsSource="{Binding EquipRange}" BackgroundColor="Transparent">
         
        <ListView.ItemTemplate>
            <DataTemplate>

                <TextCell TextColor="black" Text="{Binding ResourceName}">
                    <TextCell.BindingContext>
                        <viewmodels:EquipementViewModel/>
                    </TextCell.BindingContext></TextCell>
            </DataTemplate>
        </ListView.ItemTemplate>
        
    </ListView>
  
</ContentPage> 

I want for the TextCell to show the ResourceNasme of the Equipment class, but the VIsual Studio shows me an error that Member not found in the Data Context "Equipment View Model".

I watched numerous guides, and I don't think I'm doing something wrong. What could be the issue?


Solution

  • you don't need to supply a BindingContext for the TextCell. It will inherit the correct BindingContext from the ListView automatically

        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell TextColor="black" Text="{Binding ResourceName}">
                    <TextCell.BindingContext>
                        <viewmodels:EquipementViewModel/>
                    </TextCell.BindingContext></TextCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    

    instead just do this

        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell TextColor="black" Text="{Binding ResourceName}" />
            </DataTemplate>
        </ListView.ItemTemplate>