I'm ad ASP.NET developer but recently develop WPF applications too. In ASP.NET whenever I needed to refresh data in a GridView I just call DataBind()
. But what is alternative of it in WPF? As a work-around I currently implemented INotifyCollectionChanged
to refresh data.
You shouldn't think of implementing INotifyCollectionChanged
as a workaround - this is actually good practice in WPF.
(Note that you could populate a System.ComponentModel.BindingList instead. WPF controls that are bound to this list will automatically update as the list changes.)
So why is explicit data binding the standard practice in ASP.NET, while the observer pattern is used in WPF? This comes from the different nature of the two environments.
ASP.NET is all about creating and populating a web page. The web page doesn't incrementally update itself; when the user performs a postback a whole new webpage is recreated. Because of this, it's efficient to take a batching approach: assemble all of the data, and then say "OK, the data is ready, populate the controls now".
In a rich-client application, the user interface controls are created once, but the underlying data changes within the controls' lifetime. If you employed explicit data binding, each time the data changes you would determine what controls have to update and then call "DataBind" on each one of them. That's a maintenance headache. It's better that the only components that are aware of the binding are the controls themselves. The data can just announce "I have changed", and the controls can update themselves at their own discretion.