Forum Xamarin.Forms

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 bind the data Dynamically in the Static Listview

Vasanthakumar06Vasanthakumar06 Member ✭✭✭
edited March 1 in Xamarin.Forms

Hi Techie,
I'm New to xamarin forms . How to bind the data dynamically in the Static Listview. As of now i bind the data statically . I need to call Webservice and How i need to bind in the static listview .

Model.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
namespace FormsApp
{
public class Model: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public Model()
    {
        IsChecked = false;
    }
    public string Text { get; set; }
    public string Id { get; set; }
   private bool isChecked;
    public bool IsChecked {
        get {
            return isChecked;
        }
        set {
            isChecked = value;
            NotifyPropertyChanged();
        }
    }
}

}

myviewmodel.cs

using System;
using System.Collections.Generic;
using System.Text;
namespace FormsApp
{
public class MyViewModel
{
public static List list = new List() {
new Model() { Text = "A" ,Id="1"},
new Model() { Text = "BBB" ,Id="2"},
new Model() { Text = "BBB" ,Id="3"},
new Model() { Text = "D" ,Id="4"}
};
}
}

using Rg.Plugins.Popup.Extensions;
using Rg.Plugins.Popup.Pages;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace FormsApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page2 : PopupPage
{
public Page2()
{
InitializeComponent();
this.BackgroundColor = Color.White;
listView.ItemsSource = MyViewModel.list;
}
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PopPopupAsync();
var result = MyViewModel.list.Where(w => w.IsChecked == true).ToList();
string s = "";
int index = 0;
foreach(var model in result) {
s = s + model.Text;
if(index < result.Count - 1) {
s = s + ",";
}
index++;
}
MessagingCenter.Send<object,string>(this,"Hi",s);
}
}
}

Best Answer

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    I need to call Webservice and How i need to bind in the static listview .

    You can just change the type of the list to ObservableCollection, just as follows:

       public class MyViewModel
    {
        public static ObservableCollection<Model> list = new ObservableCollection<Model>() {
        new Model() { Text = "A" },
        new Model() { Text = "B" },
        new Model() { Text = "C" },
        new Model() { Text = "D" }
        };
    }
    

    And we can add the new items to the list after we get the result from service. And once we add new item to the list, the UI will refresh automatically.

      MyViewModel.list.Add(new Model {Text="G" });
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Answers

  • projects2016projects2016 Member ✭✭

    you should be using observablecollection instead of list

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    I need to call Webservice and How i need to bind in the static listview .

    You can just change the type of the list to ObservableCollection, just as follows:

       public class MyViewModel
    {
        public static ObservableCollection<Model> list = new ObservableCollection<Model>() {
        new Model() { Text = "A" },
        new Model() { Text = "B" },
        new Model() { Text = "C" },
        new Model() { Text = "D" }
        };
    }
    

    And we can add the new items to the list after we get the result from service. And once we add new item to the list, the UI will refresh automatically.

      MyViewModel.list.Add(new Model {Text="G" });
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Sign In or Register to comment.