locked
Convert List to Observablecollection RRS feed

  • Question

  • User397066 posted

    Dear Techie,

    How to convert List to Observablecollection in Xamarin forms and bind into Listview.

    Monday, March 1, 2021 3:15 AM

Answers

  • User371688 posted

    How to convert List to Observablecollection in Xamarin forms and bind into Listview.

    Do you want to change the type of list in the following code to Observablecollection?

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

    You can directly change it to Observablecollection, just as follows:

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

    Or convert the List to Observablecollection type just as tareqelzoubi mentioned :

    ObservableCollection<Model> myList = new ObservableCollection<Model>(list);
    

    And if you want to add list data from service, you can just convert it to the ObservableCollection list as above method or iterate, add each item in turn.

       ObservableCollection<Model> mList = new ObservableCollection<Model>();
    
        foreach(var item in list)
                mList.Add(item);
    

    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.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, March 2, 2021 2:29 AM

All replies

  • User392037 posted

    Hi @Vasanthakumar06 ObservableCollection<Object> myObservableCollection = new ObservableCollection<Object>(yourlist); or loop throw your list and add them to the ObservableCollection foreach(var item in yourlist) myObservableCollection.Add(item);

    Monday, March 1, 2021 4:31 AM
  • User371688 posted

    How to convert List to Observablecollection in Xamarin forms and bind into Listview.

    Do you want to change the type of list in the following code to Observablecollection?

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

    You can directly change it to Observablecollection, just as follows:

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

    Or convert the List to Observablecollection type just as tareqelzoubi mentioned :

    ObservableCollection<Model> myList = new ObservableCollection<Model>(list);
    

    And if you want to add list data from service, you can just convert it to the ObservableCollection list as above method or iterate, add each item in turn.

       ObservableCollection<Model> mList = new ObservableCollection<Model>();
    
        foreach(var item in list)
                mList.Add(item);
    

    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.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, March 2, 2021 2:29 AM