Search code examples
c#listviewmaui

trouble to dysplay listview content


I work on a MAUI windows desktop app. I have a problem with a listView. My datas are loaded correctly after my request in the DB.In this case I have a list of 8 objects. On the front the listview is sized with 8 slots my listview.count = 8. But nothing appears in it.
my view :

<?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="FntrAudit.Views.Modal.DocAnliszedModal"
             xmlns:model="clr-namespace:FntrAudit.Models"
             Title="DocAnliszedModal">
    <VerticalStackLayout>
      
            <VerticalStackLayout>
                <Label Text="Documents analysés" FontSize="Large"></Label>

                <ListView Header="cbcx" ItemsSource="{Binding AnalyzedDocuments}">
                    <ListView.ItemTemplate>
                        <DataTemplate >
                           
                                <!-- <CheckBox IsChecked="{Binding IsOk}" /> -->
                            <Label Text="{Binding Intitule}"></Label>
                           
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
               
            </VerticalStackLayout>
          
      
        <Button Text="quit" Clicked="Delete_Clicked"></Button>
    </VerticalStackLayout>
</ContentPage>

the code behind :

using FntrAudit.Data;
using FntrAudit.Models;



namespace FntrAudit.Views.Modal;

public partial class DocAnliszedModal : ContentPage
{
    
    private Audit _audit;
    SqliteDbContext _db = new SqliteDbContext();
    private List<AnalyzedDocument> _analyzedDocuments;
    public List<AnalyzedDocument>AnalyzedDocuments
    {
        get { return _analyzedDocuments; }
        set
        {
            if (_analyzedDocuments != value)
            {
                _analyzedDocuments = value;
                OnPropertyChanged(nameof(AnalyzedDocuments));

            }

        }
    }

    public DocAnliszedModal(Audit audit)
    {
        InitializeComponent();
        _audit = audit;       
        LoadAnalyzedDocuments();
        this.BindingContext = this;
    }
    private void LoadAnalyzedDocuments()
    {
        AnalyzedDocuments = _db.AnalyzedDocument.ToList();
        OnPropertyChanged(nameof(AnalyzedDocuments));
    }
    private void Delete_Clicked(object sender, EventArgs e)
    {
        Application.Current.MainPage.Navigation.PopModalAsync();
    }
}

the model :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FntrAudit.Models
{
    public class AnalyzedDocument : INotifyPropertyChanged
    {
       private int id;
       private string intitule;
       private bool isOk;
       private int auditId;

       public int AuditId
        {
            get { return auditId; }
            set { auditId = value; OnPropertyChanged(nameof(AuditId)); }
        }

        public int Id
        {
            get { return id; }
            set { id = value; OnPropertyChanged(nameof(Id)); }
        }

        public string Intitule
        {
            get { return intitule; }
            set { intitule = value; OnPropertyChanged(nameof(Intitule)); }
        }
        public bool IsOk
        {
            get { return isOk; }
            set { isOk = value; OnPropertyChanged(nameof(IsOk)); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

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

and the picture enter image description here

Thank a lot by advance


Solution

  • If you want to customize the appearance of the item in ListView, you may use a ViewCell.

    So just add <ViewCell> inside the <DataTemplate> tag, then you could customize the appearance of item in it.

                <ListView Header="cbcx" ItemsSource="{Binding AnalyzedDocuments}">
                    <ListView.ItemTemplate>
                        <DataTemplate >
                            <ViewCell>
                                <StackLayout>
                                     <!-- <CheckBox IsChecked="{Binding IsOk}" /> -->
                                    <Label Text="{Binding Intitule}"></Label>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
    

    Then the item will show up.

    For more info, you may also refer to ListView Cells

    Hope it helps!