locked
ListBox Items Updated every time ListBox.itemsource updated

    Question

  • 1. i created a listbox in xaml

    2.then in my mainpage.cs file i set listbox.itemsource = objects(basically a List of five object)

    3.then again when next five object come in the Listagain assign listbox.itemsource that five object and previous object are vanishes.

    basically it update the listbox.itemsource but i am looking to add that five object.

    is there any method that previous object are still appears and next coming records are add after that five in the listbox

    :)


    MobinYaqoob

    Wednesday, June 11, 2014 9:49 PM

Answers

  • If you set the ItemsSource to an ObservableCollection<> instead of to a List<> then the collection will fire change notifications and update the list without needing to be rebound:

        public sealed partial class MainPage : Page
        {
            ObservableCollection<string> data;
            int index = 0;
            public MainPage()
            {
                this.InitializeComponent();
                data = new ObservableCollection<string>();
                AddData(data, 5);
                myList.ItemsSource = data;
    
            }
    
            private void AddData(ObservableCollection<string> data, int num)
            {
                for (int i=0;i<num;i++)
                {
                    data.Add("Item " + (++index).ToString());
                    Debug.WriteLine("Added {0} total {1}", data.Last(), data.Count);
                }
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                AddData(data, 5);
            }
        }

    Thursday, June 12, 2014 1:33 AM
    Owner

All replies

  • i any  body share an example its very helpful for understanding for me :)

    MobinYaqoob

    Wednesday, June 11, 2014 9:50 PM
  • List<yourItem> ListOfAllObjects = new List<yourItem>(); List<yourItem> ListOfNewObjects = new List<yourItem>(); foreach(var o in ListOfNewObjects) { ListOfAllObjects.Add(o); }

    YourListBox.ItemsSource = ListOfAllObjects;

    //you could also try this

    //ListOfAllObjects.AddRange(ListOfNewObjects.ToList());

    Try this when your new items arrive.

    For more a more advanced solution, look up how to bind an ObservableCollection using MVVM.



    Wednesday, June 11, 2014 10:28 PM
  • If you set the ItemsSource to an ObservableCollection<> instead of to a List<> then the collection will fire change notifications and update the list without needing to be rebound:

        public sealed partial class MainPage : Page
        {
            ObservableCollection<string> data;
            int index = 0;
            public MainPage()
            {
                this.InitializeComponent();
                data = new ObservableCollection<string>();
                AddData(data, 5);
                myList.ItemsSource = data;
    
            }
    
            private void AddData(ObservableCollection<string> data, int num)
            {
                for (int i=0;i<num;i++)
                {
                    data.Add("Item " + (++index).ToString());
                    Debug.WriteLine("Added {0} total {1}", data.Last(), data.Count);
                }
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                AddData(data, 5);
            }
        }

    Thursday, June 12, 2014 1:33 AM
    Owner