To use Data Binding in WPF implement INotifyPropertyChanged (INPC) for your datasource (typically a POCO object) which will serve as the datacontext for your View.
Implementing INPC requires calling an event called PropertyChanged from within your POCO class, whenever one of its property changes. The signature for the event is
public event PropertyChangedEventhandler PropertyChanged;
To call this event, by convention we define a helper method called OnPropertyChanged like so
private void OnPropertyChanged([CallerMemberName] string title="")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(caller));
}
}
This helper method should be called from every property setter in the POCO class and that's how we get Data Binding in WPF.
private string _title;
public string Title
{
get { return _title;}
set {
_title = value;
OnPropertyChanged();
}
}
Simple and neat! Do ko.observable() and $watch sound familiar?
No comments:
Post a Comment