Hi Marc,
Thanks for the reply.
I just started looking at C# last month in my spare time. So it looks like I
completely misunderstood databinding. I thought that was the whole point of
it (i.e. some sort of subject-observer pattern), where by specifying the
binding would handle the updating, registering of observer, notification
etc.
The code I have seen in the past with databinding was always with dataSets
and not with object properties.
So, I still need the data binding code -> label1.DataBindings.Add(new
Binding("Text", directorySweepObj,
"numberOfFilesDeleted"));
and I then introduce the notification code in my object? Is that correct?
Thanks once again.
Sham.
"Marc Gravell" <mg******@rm.com> wrote in message
news:uf**************@TK2MSFTNGP09.phx.gbl...
Your object needs to tell the UI that it has changed; this can be done in
two ways:
1.1 style: create a public event on your class called
{propertyname}Changed, and call this event when your values change
2 style: implement the INotifyPropertyChanged interface, and call this
event (specifying the name of the property) when the values change
Following is an example of the latter:
public class MyClass : INotifyPropertyChanged {
private int someField;
public int SomeProperty {
get {
return someField;
}
set {
if (someField != value) {
someField = value;
OnPropertyChanged("SomeProperty"); // oh for missing
the nameof(SomeProperty) {or nameof(method)?} function...
}
}
}
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}