PropertyChangedEvent

G

Guest

I read this article called "Windows Forms Data Binding and Objects" by
Rockford Lhotka wich in an MSDN article. It makes a custom class that can be
bound to. If a value in the class changes from code or an other control it
needs to be updated in all bound controls. That´s where the PropertyChanged
event comes in. The article says that de bindingmanager listens for those
events.

Now, the sample code (I didn´t run it) is in VB.Net. I would like to do the
same thing in C#. And guess what, it doesn´t work. I don´t know what i am
doing wrong and can´t find any samples.

Anyone there that can give me a hint?

Much appreciated, thanks.
 
M

MarkR

It sounds like you would like to define a custom event -- PropertyChanged --
in your class. To do so, in your class definition you must declare the event
and then you must raise it whenever you want subscribers informed (in this
case, inside the properties of the class. Like so...

public class MyCustomClass()
{
...
public event EventHandler PropertyChanged;
...

public int Width
{
get
{
return m_width;
}
set
{
m_width = value;
if (PropertyChanged != null)
this.PropertyChanged(this, EventArgs.Empty);
}
}
}

Then in your client class, you register for the PropertyChanged event
like...

MyCustomClass myClass = new MyCustomClass();
myClass.PropertyChanged += new EventHandler(respondToPropertyChange);

public void respondToPropertyChange(object sender, EventArgs e)
{
Debug.WriteLine("MyCustomClass has changed a property.");
}


If you wish to send more information, like which property has changed, you
should derive a class from EventArgs with additional properties (like
"string PropertyAffected"), create a custom delegate, and update your
event... you should be able to find more info about that in the docs.

Good luck.
/m
 
P

Phil Wright

You need to create an event per property that is exposed for binding. So if
you create a property called Test then you need to add an event called
TestChanged. If you add another property called Widget then you need a
property called WidgetChanged. When your bound against the framework
automatically looks for an event with the same name as the property but with
the Changed extension.

You can see this in action by looking at any control in the property browser
at design time and then look at the events. You will notice a whole group of
xxxChanged events.

Phil Wright
Follow my C# component microISV startup at...
http://componentfactory.blogspot.com
 
G

Guest

Hello Mark,

Thanks for the answer. This is the link to the article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet02252003.asp

Under Notification of Changed Properties is the following sentence: When we
bind a control to a property on our object, data binding automatically starts
listening for a property changed event named propertyChanged, where property
is the name of the property of the object

I a aware of the fact that I can use custom events. But i don´t want to
write code to update my gui when databinding should be able to do so. Check
the beginning of the article. No need to go through the whole thing. Now the
same thing (or functionality) but in C#. So is the data binding indeed
listening?

Sorry I wasn´t clear in the first place.

Johan
 

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