data binding a simple value

S

sham

Hi to all,

I have an object that has a property numberOfFilesDeleted which is an
integer. I want to bind a label to it so that when the value changes, the
label gets updated on the screen.

I have used the following code to bind the text property of the label to the
object property.

private void WinForm_Load(object sender, System.EventArgs e)
{
label1.DataBindings.Add(new Binding("Text", directorySweepObj,
"numberOfFilesDeleted"));
}


When I do the following :
private void button1_Click(object sender, System.EventArgs e)
{
directorySweepObj.numberOfFilesDeleted =
directorySweepObj.numberOfFilesDeleted + 1
}

I would expect the label to be updated, but it is not.

What am I doing wrong?

Sham.
 
M

Marc Gravell

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;
}
 
S

sham

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.
 
M

Marc Gravell

Correct;

IIRC DataSets already include this functionality, which is why you haven't
seen it so far. If you think about it. it makes sense: you don't want the UI
constantly probing your objects to see which properties have changed.

For ref, if you bing to a BindingSource instead (which sits between the UI
and the objects) you can call ResetBindings() on the source to force a
re-read from the object (e.g. when you think it has changed), but IMO it is
better to use notification - this avoids a lot of problems.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top