0
votes

I have an MVVM project in which the main view ("View1") has a button called Save and listbox control which has binding of model emp.

The viewmodel (ViewModel1) implements inotifypropertychanged and it has SavedButtonCommand binding to Save button of View1 and saves the record to SavedEMp.In View1 there is a button "Next" which takes to the "View2" page.View2 page has ViewModel2 which has SaveAgain button .And viewmodel2 has a binding to the SaveAgainButtonCommand and it also saves the record to the localdb called SavedEmp of emp records.

View1 listbox gets refreshed when the statement RaisePropertyChanged("SavedEmpDataSoruce") gets executed and the observablecollection "SavedEmpDataSoruce" is binding to this listbox.Since these two are in View1 after the save button whenenever the statement RaisePropertyChanged("SavedEmpDataSoruce") gets executed it is refreshing the data.

But when user comes back from View2 to View1 the listbox in View1 is not refreshed with the data of View2 saved emp record.

    private ObservableCollection<Emp> _SavedEmp;
    public ObservableCollection<Emp> SavedEmp
    {
        get
        {

            if(_SavedEmp == null)
                {                       
                   _SavedEmp  = Emp.GetSavedEmps();

                }
            return _SavedEmp;                

        }
        set
        {
            this._SavedEmp = value;
            RaisePropertyChanged("SavedEmp");
        }
    }

Is there a way we can refresh the listbox data when moved from View2 to View1 again ?

1

1 Answers

1
votes

Communication between dependent views can be accomplished using a Messenger-style object. An implementation of this object comes standard with the MVVM Light toolkit if you are making use of it. The pattern consists of one object registering with the messenger to receive messages of a given type at a provided callback, and another object dispatching a message of that type. The messenger maintains the lists of addressees for these messages and delivers them accordingly as parameters to the callback function provided by the recipient. A sample implementation using MVVM Light's messenger follows:

// Message container
public class AccountChangedMessage : GalaSoft.MvvmLight.Messaging.GenericMessage<Account>
{
    public AccountChangedMessage(Account a) : base(a) { }
}

// Dependent ViewModel
public class AccountsViewModel : GalaSoft.MvvmLight.ViewModelBase
{
    public AccountsViewModel()
    {
        MessengerInstance.Register<AccountChangedMessage>(this, OnAccountChanged);
    }

    private void OnAccountChanged(AccountChangedMessage msg)
    {
        // TODO: Rebuild bound data
    }
}

// Initiating ViewModel
public class AccountEditViewModel : GalaSoft.MvvmLight.ViewModelBase
{
    public void SaveAccount()
    {
        _accountService.Save(_account);

        MessengerInstance.Send(new AccountChangedMessage(_account));
    }
}

In a sense, this is akin to raising events and registering listeners for those events, but the model is very powerful and much more disconnected, so please use it as sparingly as possible, as this does create a maintenance concern for programmers who come behind you and attempt to follow control flow.