serhio
serhio

Reputation: 28586

Refresh the GUI from DataSource in Silverlight

I have an object MyPerson with the properties FirstName, LastName, and FullName where

public string FullName
{
    get {return LastName + " " + FirstName;}
    set {...}
}

I bind the MyPerson to a UserControl, in which I bind FirstName, LastName and FullNAme to 3 texboxes.

Now, when I modify the FirstName or LastName I need to indicate to the UserControl to "update" the FullName.

What should be this "update" command?

Silverlight 4

Upvotes: 2

Views: 112

Answers (2)

Botz3000
Botz3000

Reputation: 39630

Your view model should implement the INotifyPropertyChanged Interface, so it can notify the view of the changed properties.

Look here for more info:
http://msdn.microsoft.com/en-us/library/ms229614.aspx

Also, if any of first or last name changes, you'd need to notify of a change in fullname, too.

Upvotes: 1

Brad Christie
Brad Christie

Reputation: 101614

You should probably look in to the INotifyPropertyChanged interface. This will make your life MUCH easier.


Example:

public class Person : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  private void NotifyPropertyChanged(String info)
  {
    if (PropertyChanged != null)
    {
      PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
  }

  private String _FirstName;
  private String _LastName;

  public String FirstName
  {
    get
    {
      return this._FirstName;
    }
    set
    {
      if (this._FirstName != value)
      {
        this._FirstName = value;
        this.NotifyPropertyChanged("FirstName");
        this.NotifyPropertyChanged("FullName");
      }
    }
  }

  public String LastName
  {
    get
    {
      return this._LastName;
    }
    set
    {
      if (this._LastName != value)
      {
        this._LastName = value;
        this.NotifyPropertyChanged("LastName");
        this.NotifyPropertyChanged("FullName");
      }
    }
  }

  public String FullName
  {
    get
    {
      return String.Format("{0} {1}", this.LastName, this.FirstName);
    }
  }
}

Upvotes: 3

Related Questions