Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to display List property In nested object c# MAUI

I’m working on an app in MAUI C#. Here is my code:

View :

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="testbinding.MainPage"
             xmlns:local="clr-namespace:testbinding.ViewModel"
             x:DataType="local:TestViewModel">

    <ScrollView>
        <VerticalStackLayout>
            <CollectionView x:Name="Buttons" ItemsSource="{Binding TestModel.Toto}" EmptyView="pas de data" IsGrouped="True">
                <CollectionView.ItemTemplate >
                    <DataTemplate >
                        <VerticalStackLayout Spacing="300000" >
                            <Label Text="{Binding Plop}"></Label>
                        </VerticalStackLayout>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

The viewModel :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

namespace testbinding.ViewModel
{
    public class TestViewModel : INotifyPropertyChanged
    {
        public TestModel TestModel { get; } = new TestModel();


        public TestViewModel()
        {
            TestModel = //call api that return the right object
            
        }

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string propertyName) =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  
    }
}

the model :

namespace testbinding.Model
{
    public class TestModel : INotifyPropertyChanged
    {
       private string id;
       private string name;
       private List<Toto> toto;
        public string Id 
        { 
          get { return id; }
          set { id = value; OnPropertyChanged(nameof(Id)); }
        }

        public string Name
        {
            get { return name; }
            set { name = value; OnPropertyChanged(nameof(Name)); }
        }

        public List<Toto> Toto
        {
            get { return toto; }
            set { toto =  value; OnPropertyChanged(nameof(Toto)); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string name) =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    
    }
}

and the model in list:

namespace testbinding.Model
{
    public class Toto : INotifyPropertyChanged
    {
        private int id { get; set; }
        private string  plop { get; set; }
        public int Id 
        { get { return id; }
          set { id = value; OnPropertyChanged(nameof(Id)); }
        }
        public string  Plop
        {
            get { return plop; }
            set { plop = value; OnPropertyChanged(nameof(Plop)); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string name) =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

I’m new with MVVM and I want to display the property Plop in a label in collectionView. I have this Error: Binding: Property "Plop" not found on "testbinding.ViewModel.TestViewModel". testbinding C:\sources\testbinding\MainPage.xaml.

I don’t understand the problem my property is in the model Toto and the call API return the right object with the datas.

>Solution :

you need to add an xmlns for your models

xmlns:model="clr-namespace:testbinding.Model"

then add this to your DataTemplate

<DataTemplate x:DataType="model:Toto" >
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading