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

ListView binding using xaml in xamarin forms

$
0
0

Hi, I am a beginner of Xamarin forms.
I am working with list view bindings as part of my xamarin forms. I could not able to see the data updated to list view even after data binding is done through xaml. With debugging i observed that, PropertyChanged event is null. The reason for getting this event null is - "Not setting the data context properly". I set the data context as well but no data is populated in list view.

Here is my code.

SampleCode.Xaml


<ContentPage.Content>


<ListView.ItemTemplate>


<ViewCell.View>

</ViewCell.View>


</ListView.ItemTemplate>

</ContentPage.Content>

ViewModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace SampleNS
{
public class ViewModel : INotifyPropertyChanged
{
private string _name;

    public string Name
    {
        get {return _name;}
        set {
                if (value.Equals(_name, StringComparison.Ordinal))
                {
                    return;
                }
                _name = value;
            OnPropertyChanged(_name);
            }

    }

    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged ([CallerMemberName] string propertyName=null)
    {
        var handler = PropertyChanged;
        if (handler != null) {
            handler (this, new PropertyChangedEventArgs (propertyName));
        }
    }
}

}

SampleCode.Xaml.cs

using System;
using System.Collections.Generic;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace SampleNS
{
public partial class SampleCode : ContentPage
{
public static List lsData;

    private void init()
    {   

        //this.BindingContext = lsData;
        this.BindingContext = new ViewModel();


        this.LoadFromXaml (typeof(SampleCode));

        lsData = new List<ViewModel> () {
                            new ViewModel { Name = "VM1" },
                            new ViewModel { Name = "VM2" },
                            new ViewModel { Name = "VM3" },
                            new ViewModel { Name = "VM4" },

}

I assumed BindingContext is same as DataContext in WPF. (Please suggest if i am wrong)

I also tried setting the binding context in the xaml directly like below. But i am getting the run time file not found exception saying "Could not load the file or assembly 'SampleNS' or one of its dependencies.

<ContentPage.BindingContext>

</ContentPage.BindingContext>

Now i got two problems. One is the list view binding the other is the setting the xmlns:local attribute. (I am using the shared project)

I doubt about setting the BindingContext. Please suggest me, where i am doing the mistake. Presently my PropertyChanged event is null and i don't see any data in my list view.

Thanks in advance.


Viewing all articles
Browse latest Browse all 58056

Trending Articles



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