By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

data binding a simple value

P: n/a
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.
Jan 31 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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;
}
Jan 31 '06 #2

P: n/a
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;
}

Jan 31 '06 #3

P: n/a
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
Jan 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.