Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to display an ObservableCollection in a listview

Hello
i have an observablecollection
private readonly ObservableCollection collection1 = new ObservableCollection();

namespace app1
{
class Classdata1
{
public string name1 { get; set; }
public string par1 { get; set; }
public string civildate { get; set; }
}
}

how to bind collection 1 to a listview in xaml ?
Thanks

Tagged:

Best Answer

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited November 2020

    @shayw You could create a ViewModel class to add the ObservableCollection<object> perperty. Make sure the property is public, or it cannot be got in the page.

    public class CustomViewModel
    {
        public ObservableCollection<CustomModel> DataCollection { get; set; }
        public CustomViewModel()
        {
            DataCollection = new ObservableCollection<CustomModel>();
    
            //add the data
        }
    }
    

    Then set BindingContext to the page with the custom ViewModel class.

    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent();
    
            BindingContext = new CustomViewModel();
        }
    }
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Xamarin.Android???

    You should use RecyvlerView and adapters....

  • shaywshayw Member ✭✭

    @Jarvan
    ListView ItemsSource attribute doesn't exist in xamarin .android

Sign In or Register to comment.