1
votes

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 
    Title="ListViewBinder" 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:SampleNS;assembly=SampleNS" 
    x:Class="SampleNS.SampleCode">
<ContentPage.Content>
        <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand" >
            <ListView  x:Name= "listView" ItemsSource="{Binding lsData}" >
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <ViewCell.View>
                                            <Label Text="{Binding Name}"></Label>
                                        </ViewCell.View>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                    </ListView>
                </StackLayout>
</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<ViewModel> 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 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.

1
I'd try setting BindingContext after LoadFromXaml. - Miha Markic
Hi Miha, Thanks for your reply. I tried moving down the binding context. Still i see PropertyChanged event is null in ViewModel class and my list view is not updated. :-( - siva Rapolu

1 Answers

0
votes

you sample code.xaml.cs should look like this:

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

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

        public SampleCode ()
        {   
            InitializeComponent ();

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