henda79
henda79

Reputation: 535

How to update Xamarin Data Binding when source changed

I have 2 labels which are data bound to an object on my profile overview page, lets assume:

public class Profile
{
    string FirstName { get; set; }
    string LastName {get; set; }
}

I have a profile edit page which allows the user to make changes to the properties. In the View Model for this page we are passed the Profile object and we make a clone of this so we are able to Cancel any changes without affecting the data bound labels if the user chooses not to Save.

However, when the user saves the changes the cloned Profile object gets passed back to the calling View Model, now how can I update the original Profile object using the cloned one as something as simple as:

originalProfile = clonedProfile;

does not work as the data bindings are not updated which is expected behaviour. So short of manually updating property by property, what's the best way to achieve this so the data bound controls are updated ?

Hope this makes sense.

Upvotes: 0

Views: 780

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

You need to implement the interface INotifyPropertyChanged both in Model and ViewModel so that the UI will update in runtime .

in ViewModel

public class xxxViewModel: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;


  
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }


   

    Profile _originalProfile ;

    Profile originalProfile { 
        
        get
        {
            return _originalProfile ;
        }

        set
        {
            if(_originalProfile !=value)
            {
                _originalProfile = value;
                OnPropertyChanged("originalProfile");
            }
        }
    }




}

in Model

public class Profile: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    
  
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }


    string firstName ;

    string FirstName { 
        
        get
        {
            return firstName ;
        }

        set
        {
            if(firstName !=value)
            {
                firstName = value;
                OnPropertyChanged("FirstName");
            }
        }
    }

    string lastName ;

    string LastName { 
        
        get
        {
            return lastName ;
        }

        set
        {
            if(lastName !=value)
            {
                lastName = value;
                OnPropertyChanged("LastName ");
            }
        }
    }
}

Update

You could add a method in Profile

// 
void UpdateValue(Profile newValue)
{
  this.FirstName = newValue.FirstName;
  //...
}

Now in ViewModel you just need to invoke it like following .

originalProfile.UpdateValue(clonedProfile);

Upvotes: 2

Related Questions