2
votes

I'm writing a WPF/MVVM application using Prism/Unity. I'm having issues on how to transfer data from one usercontrol to another when navigating a region.

I have a region

This is populated by a user control("ContactsList") solely containing a grid, this grid is bound by ItemsSource & SelectedItem.

I want to navigate to ContractEdit and pass the Contact bound to SelectedItem.

LocalRegionManager.RequestNavigate(ContactRegions.MainRegion, ContactsUri.ContactsEdit);

I can pass a single value in he Datacontext or the Uri but I don't want to have to go back to the Database to get the data when I already have the whole Item in the list view model.

How can I pass a whole object from the list usercontrol to the edit control?

Thanks in advance.

Conclusion

I added the EventAggregator

  public ListViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
  {
    _eventAggregator = eventAggregator;
    _regionManager = regionManager;
  }

Published the even when navigating

private void OnSendData()
{
    _regionManager.RequestNavigate(ShellRegions.LeftRegion, ModuleAUris.Edit);
    _eventAggregator.GetEvent<UserEvent>().Publish(_selectedItem);
}

Then subscribed to the even to get the object.

 public EditViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
    {
        _regionManager = regionManager;
        _eventAggregator = eventAggregator;

        BackCommand = new DelegateCommand(OnBack);

        _eventAggregator.GetEvent<UserEvent>().Subscribe((e) => UserEntity = e);
    }
2

2 Answers

0
votes

There is a handy example of this in the prism examples downloaded with the prism installer; where they use a separate file for subscribing/unsubscribing to an event. There's a desktop and Silverlight version.

It shows how to create a token used to unsubscribe from the event also.

Oli

0
votes

You can have a NavigationManager that will know what is the current Contact. You just have to set it and get it for example in the NavigatedFrom and NavigatedTo, or somewhere else. It can either be in its own module or in the core project, as long as CurrentContact is an IContact.

You can also have a service in the list module that will access the current contact from other modules.

The service might be more appropriate if te NavigationManager has no other purpose.

edit

These services explained on msdn.