Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi, now i am trying to bind collection of class properties to list view. facing same same issue again, code runs ok but nothing displays on screen. any help will be deeply appreciated.


What I have tried:

C#
using System.Collections.ObjectModel;
using System.Reflection.Metadata.Ecma335;
using System.Security.Cryptography.X509Certificates;

namespace MauiApp12;
public partial class MainPage : ContentPage
{
    public class contacts
    {
        //https://stackoverflow.com/questions/11317587/return-a-string-from-a-method-in-c-sharp
        public string FirstName { get; set; }
    }
    //public contacts x = new contacts() { FirstName="what is your name"};
    public ObservableCollection<contacts> x { get; set; } = new ObservableCollection<contacts> { 
    new contacts(){FirstName="name1"},
    new contacts(){FirstName="name2"},
    new contacts(){FirstName="name3"},
    new contacts(){FirstName="name4"},
    new contacts(){FirstName="name5"}
    };
    public MainPage()
    {
        InitializeComponent();
        BindingContext = this;
}
    private async void OnCounterClicked(object sender, EventArgs e)
    {

        //await DisplayAlert("alert",x.fullNameMethod(), "ok");
        lv.SetBinding(ItemsView.ItemsSourceProperty, "FirstName");
    }
    }


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="MauiApp12.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">
            <ListView x:Name="lv" ItemsSource="{Binding x}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid Padding="10">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Label Grid.Column="1"
                           Text="{Binding FirstName}"
                           FontAttributes="Bold" />
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <Image
                Source="dotnet_bot.png"
                SemanticProperties.Description="Cute dot net bot waving hi to you!"
                HeightRequest="200"
                HorizontalOptions="Center" />

            <Label
                Text="Hello, World!"
                SemanticProperties.HeadingLevel="Level1"
                FontSize="32"
                HorizontalOptions="Center" />

            <Label
                Text="Welcome to .NET Multi-platform App UI"
                SemanticProperties.HeadingLevel="Level2"
                SemanticProperties.Description="Welcome to dot net Multi platform App U I"
                FontSize="18"
                HorizontalOptions="Center" />

            <Button
                x:Name="CounterBtn"
                Text="Click me"
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnCounterClicked"
                HorizontalOptions="Center" />

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
Posted
Updated 5-Jun-23 8:57am

1 solution

Thre links provided in the previous answer contain the information to answer this question. Also, James has a great set of YT videos dedicated to MAUI. I highly recommend that you read the material and watch his videos: .NET MAUI Tutorial for Beginners - Build iOS, Android, macOS, & Windows Apps with C# & Visual Studio - YouTube[^]

However, here is an example of what you are trying to do:

1. Code-Behind - same as previous example, however using a collection of objects:
C#
public partial class MainPage : ContentPage
{
	int count = 0;

	public MainPage()
	{
		InitializeComponent();
        BindingContext = this;
    }

    public ObservableCollection<Person> Names { get; set; } = new ObservableCollection<Person>
    {
        new Person { FirstName = "Freddie", LastName = "Kruger" },
        new Person { FirstName = "Jason", LastName = "Voorhees" },
        new Person { FirstName = "Norman", LastName = "Bates" },
        new Person { FirstName = "Hannibal", LastName = "Lecter" },
        new Person { FirstName = "Damien", LastName = "Thorn" },
    };
}

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

2. The XAML:
XML
<?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="MauiApp1.MainPage">

    <ListView ItemsSource="{Binding Names}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid Padding="10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Label Text="{Binding FirstName}"
                               Grid.Column="0"/>
                        <Label Text="{Binding LastName}"
                               Grid.Column="1"
                               Padding="10,0"/>
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>
 
Share this answer
 
v2
Comments
suhail malik 2023 6-Jun-23 1:08am    
same issue , wich i'm facing since last three days. code runs ok but no output on screen. i am pretty sure that bindingcontext has some thing to do with this but some how have not yet figured it out.
suhail malik 2023 6-Jun-23 2:13am    
displaying appname with class 5 times
maui app.mainpage+person
maui app.mainpage+person
maui app.mainpage+person
maui app.mainpage+person
maui app.mainpage+person
Graeme_Grant 6-Jun-23 2:25am    
The code posted above works as expected. Check that you have implemented correctly.
suhail malik 2023 6-Jun-23 3:51am    
yes it is working, actually i have very low vision and i use screen reader for my my work. this time my screen reader was not reading it because list view was not in string but in properties and screen reader read strings. thanks alot my friend have a beautiful life.
Graeme_Grant 6-Jun-23 5:42am    
Glad it is working for you. 😊

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900