observable collection

Observable collection built into the wpf application can help minimize your code by automatically update your list controls. Refreshes list faster when the items are added or modified.

ObservableCollection is buitin collection class in wpf that notifes UI to update the data whenever the items are changed, added or removed from the collection. The class implmenets the INotifyCollectionChanged interface to notify UI components whenever the changes is performaed on the class.

Advantages of ObservableCollection

Consider the following scenario where you want to bind a list of objects to the datalist components like listbox on wpf. Each time you add, remove or update object on list , you have to explicitly refresh the datalist component to show the updated list. To automatically refresh the view you can use Collection class that implments INotifyCollectionChanged interface. Because ObservableCollection is already implements INofityCollectionChanged interface, binding the list controls to it can get rid of explicitly refreshing the datalist.

Imagine when you want to change a single field value of a single object of  long list of a collection which is bind to the listbox in wpf. Without implementing INotifyPropertyChanged you have to loop through the entire list to update the listbox. It can take a long time to update the listbox when the collection is big. ObservableCollection helps update the list instantly. So another advantage is faster refreshing lists. 

INotifyPropertyChanged

Object inside the ObservableCollection must implment the INotifyPropertyChanged interface. Whenever the field value is changed in the object it must raise the event  "PropertyChanged" to signal the UI that the field value is changed. 
Here is the example of the class that implments INotifyPropertyChangedEvent.

		public class User : INotifyPropertyChanged

{

private string _userName;

public User()

{

UserName = "New user";

}

public string UserName

{

get { return _userName; }

set

{

_userName = value;

FirePropertyChangedEvent("UserName");

}
}

private string _firstName;

public string FirstName

{

get { return _firstName; }

set

{

_firstName = value;

FirePropertyChangedEvent("FirstName");

}

}

public event PropertyChangedEventHandler PropertyChanged;

private void FirePropertyChangedEvent(string propertyName)

{

if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

}

} 

Like us

See also

log4net: dynamic log path

Dynamically determine where to output logs at run-time.

observable collection

Observable collection built into the wpf application can help minimize your code by automatically update your list controls. Refreshes list faster when the items are added or modified.

Dictionary settings provider

Store dictionary data on config file at runtime as binary data.