(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Tip: How to implement changed event for a property that will be bound?

(0 votes)
Denislav Savkov
Denislav Savkov
Joined Feb 11, 2008
Articles:   14
Comments:   6
More Articles
0 comments   /   posted on Aug 29, 2008
Categories:   Data Binding

When the value of a property is changed it should notify the bound objects that the property value has changed. You can do that by implementing changed event.


public class Client
    private string name;
    public event EventHandler NameChanged;
    public string Name
            return this.Name;
            if ( this.name == value )
            this.name = value;
            this.OnNameChanged( EventArgs.Empty );
    protected virtual void OnNameChanged( EventArgs e )
        if ( this.NameChanged != null )
            this.NameChanged( this, e );

Note that the value is changed only if the new value is different. Thus we prevent the firing of the NameChanged event when the actual value is not changed.

Alternative way is to implement INotifyPropertyChanged interface.

That's it!



No comments

Add Comment

Login to comment:
  *      *