Johan
Fri Jun 17 15:05:10 CDT 2005
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
"MarkR" wrote:
> 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
>
>
>
>
>
> "Johan" <Johan@discussions.microsoft.com> wrote in message
> news:65612147-4D66-4D66-B934-DA5A1079C1D9@microsoft.com...
> >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.
> >
>
>
>