I would like to know why according to this article and observable collection binds significantly faster(20 ms vs 1685ms, that's 800X faster) than a List<> collection in WPF. I looked at the internals of ObservableCollection and it uses a List as it's storage collection object(I used reflector and saw this in the constructor)
public Collection()
{
this.items = new List<T>();
}
So what's going on here?
Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.
Data binding is the process that couples two data sources together and synchronizes them. With data binding, a change to an element in a data set automatically updates in the bound data set.
Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.
Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.
The comparison in that article isn't between two simple binding operations, those measurements refer to a scenario in which you add a single item to a WPF ListBox
that is already bound to either a List<T>
or an ObservableCollection<T>
.
As the author remarks:
...the CLR
List<T>
object does not automatically raise a collection changed event. In order to get theListBox
to pick up the changes, you would have to recreate your list of employees and re-attach it to theItemsSource
property of theListBox
. While this solution works, it introduces a huge performance impact. Each time you reassign theItemsSource
ofListBox
to a new object, theListBox
first throws away its previous items and regenerates its entire list.
This explains the performance difference. Even though ObservableCollection<T>
is backed by a List<T>
, it implements the INotifyCollectionChanged
interface, which renders all that extra processing unnecessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With