PRISM Update ViewModel after Resolve

Topics: Prism v4 - WPF 4
Nov 29, 2012 at 7:55 AM
Edited Nov 29, 2012 at 7:57 AM

We are creating a Metro style application with PRISM and WPF. Several tiles (different modules) are shown in a dashboard.

Since we are using the tiles on different dashboards and since we are navigating back and forth to detail views, we use Resolve to get the tile from the container.

Furthermore we are using MVVM, or at least we try to use. When I press a tile the customer will be retrieved from a webservice and the other tiles need to be updated. However, I am not sure how to do this. Every example online is based on the fact that you change the data by entering a new value in a textbox or something.

So there is:

  • RefreshTileView
  • BalanceTileView (which shows Customer.Balance from the ViewModel)
  • BalanceTileViewModel (or BalanceTileViewViewModel, which holds the Customer)

In a host module I am dynamically constructing the composite view, based on the information in the customer object. So basically, this is happening:

  • Customer customer = SomeWebservice.GetCustomer(int someCustomerId);
  • Check to see which tiles need to be loaded (based on customer properties)
  • BalanceTileView balanceTileView = container.Resolve<BalanceTileView>();
  • ??? Pass customer to balanceTileView and/or to BalanceTileViewModel ???
  • RegionManager.AddToRegion("RegionName", balanceTileView)

The constructor of the view looks like this:

 

public BalanceTileView(BalanceTileViewModel viewModel) {        
    this.DataContext = viewModel;
    InitializeComponent();
}

And the view has a DependencyProperty, like this:

 

public static readonly DependencyProperty CustomerProperty = DependencyProperty.Register("Customer", typeof(Customer), typeof(BalanceTileView));
public Customer Customer{ get { return (Customer)GetValue(CustomerProperty); }� � set� � {� � � � SetValue(CustomerProperty, value);� � }}

When I run the application my Balance property remains empty. This happens because the databinding is done when the TileView is resolved. And after resolving I pass the Customer object. 

How do I make sure that when I change the Customer object in the view, some parameter in the ViewModel is changed as well and the new Balance is reflected in the tile? I am not sure how to do this. Or should I pass the new Customer object directly to the ViewModel instead of the View? If so, how can I access the ViewModel and how can I make sure the Balance is updated in the tile?

Any help will be appreciated since most of the examples work the other way around.

Nov 29, 2012 at 10:05 AM
Edited Nov 29, 2012 at 10:06 AM

I am a little bit further. I have made a property in my ViewModel:

(ObservableHelper is a class I copied from Cinch framework)

static PropertyChangedEventArgs walletChangeArgs = ObservableHelper.CreateArgs(x => x.Wallet);

        private Wallet wallet;
        public Wallet Wallet
        {
            get { return wallet; }
            set
            {
                wallet = value;
                NotifyPropertyChanged(walletChangeArgs);
            }
        }

 

However I still need to do something like this in my View:

 

public Wallet Wallet { 
set { 
((BalanceTileViewModel)this.DataContext).Wallet = value; } }

 

Which doesn't seem like the right way.

Nov 29, 2012 at 10:09 AM
Edited Nov 30, 2012 at 4:06 AM

Hi,

Have you tried using the eventaggregator in prism? You could use that to publish information which your other tiles can subscribe to and refresh themselves.

Nov 29, 2012 at 12:41 PM

I have used it for other things and I didn't think about doing it for this problem. Thanks for the tip, it might be as simple as this.

Nov 29, 2012 at 1:40 PM

Works like a charm!

Thanks again

Nov 29, 2012 at 1:47 PM
Edited Nov 30, 2012 at 4:06 AM

Cool!