Simple list view in MAUI

BitSmithy 2,141 Reputation points
2024-10-06T19:28:15.4+00:00

I am sorry about this simple question, but I am traing to write my first code in MAUI.

I want to code simple list view which shows data from list.

My code behind:

    public partial class MainPage : ContentPage

    {

        List<string> ListsToCheck { get; set; } = new List<string>() ;

        public MainPage()

        {

            ListsToCheck.Add("Room 101");

            ListsToCheck.Add("Room 102");

            ListsToCheck.Add("Room 103");

            ListsToCheck.Add("Room 104");

            InitializeComponent();

            lvList.ItemsSource = ListsToCheck;          
}     
}

XAML

 ItemsSource="{Binding ListsToCheck}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout>
                            <Label Text="{Binding .}" />
                        </StackLayout>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

This code creates ListView, populates it with items from LIST but item string isnt trasported to the ui, in result all ListView items are empty.

How to fix my problem.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,596 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,551 Reputation points Microsoft Vendor
    2024-10-07T01:58:41.7333333+00:00

    Hello,

    Firstly, please add ViewCell in ListView's <DataTemplate> like following code.

    <ListView x:Name="lvList" ItemsSource="{Binding ListsToCheck}"  >
        <ListView.ItemTemplate>
               <DataTemplate>
    
                  <ViewCell>
    
                       <StackLayout>
                            <Label Text="{Binding .}" />
                      </StackLayout>
    
                 </ViewCell>
    
                </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    Then set your List<string> ListsToCheck to Public, remove lvList.ItemsSource = ListsToCheck; and add BindingContext = this;, your Listview will appear.

    public partial class MainPage : ContentPage
    {
        public List<string> ListsToCheck { get; set; } = new List<string>();
        public MainPage ()
    	{
    		InitializeComponent();
            ListsToCheck.Add("Room 101");
     
            ListsToCheck.Add("Room 102");
     
            ListsToCheck.Add("Room 103");
     
            ListsToCheck.Add("Room 104");
     
            InitializeComponent();
     
            // lvList.ItemsSource = ListsToCheck;
     
            BindingContext = this;
        }
    }
    

    Here is a document about Bindings and collections, you can refer to it.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.