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

C#

public class Client
{
    private string name;
    public event EventHandler NameChanged;
 
    public string Name
    {
        get
        {
            return this.Name;
        }
        set
        {
            if ( this.name == value )
                return;
 
            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!


Subscribe

Comments

No comments

Add Comment

Login to comment:
  *      *