Recommended

  • Silverlight 4 Podcast Pack with Tim Heuer
  • Building Modular Silverlight Applications
  • Prism -  10 Things to Know
  • Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques
  • Model– View – ViewModel in Silverlight
Skip Navigation LinksHome / Tips / View Tip

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

+ Add to SilverlightShow Favorites
1 comments   /   posted by Denislav Savkov on Aug 29, 2008
(0 votes)
Categories: 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!

Share


Comments

Comments RSS RSS
  • RE: Tip: How to implement changed event for a property that will be bound?  

    posted by shithead on Jan 09, 2010 23:14
    wtf

Add Comment

 
 

   
  
  
   
Please add 4 and 2 and type the answer here: