Home > Mobile >  ObservableCollection<MyModel> doesn't see property
ObservableCollection<MyModel> doesn't see property

Time:06-08

One of the properties of my model that I use in a CollectionView in my .NET MAUI app is a class. It looks like this:

public MyModel
{
   public string Name { get; set; }

   public Vehicle Car { get; set; } = new Vehicle();
}

My view model looks like this:

public class MyViewModel : BaseViewModel
{
   ObservableCollection<MyModel> data = new ObservableCollection<MyModel>();

   public ObservableCollection<MyModel> Data
   {
      get => data;
      set
      {
         if(data == value)
            return;

         data = value;
         OnPropertyChanged();
      }
   }
}

In my collection view, when I try to access one of the properties of the Car property which is a Vehicle object, I get an error that tells me that the property is not found.

<CollectionView
   Source={Binding Data}>
   <CollectionView.ItemTemplate>
      <DataTemplate>
         <StackLayout>
            <Label
               Text={Binding Car.Make} />
         </StackLayout>
      <DataTemplate>
   </CollectionView.ItemTemplate>
</CollectionView>

Any idea what could be causing this?

CodePudding user response:

Based on your code,I created a demo, and it works properly on my side.

You can refer to the following code:

MyViewModel.cs

public class MyViewModel: INotifyPropertyChanged
{
    ObservableCollection<MyModel> data = new ObservableCollection<MyModel>();

    public ObservableCollection<MyModel> Data
    {
        get => data;
        set
        {
            if (data == value)
                return;

            data = value;
            OnPropertyChanged();
        }
    }

    public MyViewModel() {
        Data = new ObservableCollection<MyModel>();

        Data.Add(new MyModel { Name ="model_1", Car= new Vehicle {Make="Make1" } });
        Data.Add(new MyModel { Name = "model_2", Car = new Vehicle { Make = "Make2" } });
        Data.Add(new MyModel { Name = "model_3", Car = new Vehicle { Make = "Make3" } });
        Data.Add(new MyModel { Name = "model_4", Car = new Vehicle { Make = "Make4" } });

    }

    bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        if (Object.Equals(storage, value))
            return false;

        storage = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

Vehicle.cs

public class Vehicle
{
    public string Make { get; set; }
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiCollectionApp.MainPage"
              xmlns:local="clr-namespace:MauiCollectionApp"
             >

    <ContentPage.BindingContext>
        <local:MyViewModel></local:MyViewModel>
    </ContentPage.BindingContext>


   <CollectionView  ItemsSource="{ Binding Data}">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout>
                            <Label Text="{Binding Car.Make}" />

                </StackLayout>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
   </CollectionView>

</ContentPage>

The result is:

enter image description here

CodePudding user response:

The compiler may be having an issue determining what the data type is for the items bound to the data template.

You may need to set x:DataType for your DataTemplate to MyModel so that it knows which properties are available.

It would look something like: <DataTemplate x:DataType="MyModel">

See your modified code:

<CollectionView
   Source="{Binding Data}">
   <CollectionView.ItemTemplate>
      <DataTemplate x:DataType="declaredNamespace:MyModel">
         <StackLayout>
            <Label
               Text="{Binding Car.Make}" />
         </StackLayout>
      <DataTemplate>
   </CollectionView.ItemTemplate>
</CollectionView>

If it continues to show you the error, you may need to clean and rebuild the solution so that the necessary files and references can be generated. Other than that, it would probably be your Vehicle class is actually missing the property, or that property isn't defined properly, but we can't see that code.

  • Related