Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Binding Context vs ItemsSource for ListView in Xamarin Forms

$
0
0

I am developing a Xamarin forms App, in which I have a class which I instantiate and give the value to the BindingContext. The issue is the listview does not show up with the values.

However, if I use the listview XAML reference name in the code behind and assign the list of the class to listview's ItemsSource property it works.

e.g. This is the C# code

public MainPage()
{
InitializeComponent();

        vm = new MainPageViewModel();            

        listview.ItemsSource = vm.People;                  
    }

public class MainPageViewModel
{
public ObservableCollection People = new ObservableCollection();

    public MainPageViewModel()
    {
        for(int i = 0; i <= 5; i++)
        {
            Person person = new Person();
            person.FirstName = "Sidd " + i.ToString();
            person.LastName = "Mehta" + i.ToString();
            People.Add(person);
        }
    }

}

public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
This is my XAML:


<ListView.ItemTemplate>



</ListView.ItemTemplate>

The above code shows the list view.

But if I use it with binding context, like the following:

XAML Code:


<ListView.ItemTemplate>



</ListView.ItemTemplate>

and C# code:

public MainPage()
{
InitializeComponent();

        vm = new MainPageViewModel();
        BindingContext = vm;                 
    }

This fails.

Any reason or help for this?. I have installed latest community edition of VS 2017, so everything is latest.

Thanks In Advance.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>