Author Topic: WPF - Difference between ObservableCollection and List  (Read 973 times)

administrator

  • Administrator
  • Full Member
  • *****
  • Posts: 69
WPF - Difference between ObservableCollection and List
« on: October 24, 2011, 12:58:29 pm »
ObservableCollection can implements INotifyCollectionChanged, INotifyPropertyChanged so that it can send the update message to dependency property UI controls. In this code snippet, gui_studentListBox is a WPF ListBox control and it’s data context is set with new StudentInformationCollection(); 

When you click on add button, a new record has been inserted into the data context. No matter StudentInformationCollection is derived from List or ObservableCollection. But it would be updated in the ListBox UI only if you derive it from ObservableCollection. If you use List, you have reset the datacontext of WPF ListBox each and every time you make changes to the collection.



public class StudentInformationCollection : ObservableCollection<StudentInformation> // preferred
{
}


public class StudentInformationCollection : List<StudentInformation>
{
}


public StudentInformationWindow()
{
      InitializeComponent();
gui_studentListBox.DataContext = new StudentInformationCollection(); 
}


private void Add_Click(object sender, RoutedEventArgs e)
{
   StudentInformationCollection collection = (StudentInformationCollection) gui_studentListBox.DataContext;
collection.Add(new StudentInformation() { Name = "Kathir", City = "San Francisco" });
}

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1