using same region multiple times & changing selected item with mvvm globally

Topics: Prism v2 - Silverlight 4
Jun 3, 2010 at 3:51 PM
Edited Jun 3, 2010 at 3:55 PM

I have a requirement -

 

I need to use a region twice in a project at two different modules, with mvvm pattern in each module. 

 

I hold a combo box in this region. I want to do a selection change globally on both the modules, when i do a selection at any one place on the application. 

 

I am able to follow mvvm pattern and using INotifyPropertyChanged, I am able to do the global change on both combos on load by holding the data on isolated storage. Second time when i try to do a change, I am able to only find change at one region, second region holds the old data. Which means i am not able to trigger the notificationchanged for the other region. 

 

 private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

 

public long currentsortitem

{

get

{

if(appSettings["currentsortitem"] != null)

{

_currentsortitem = Convert.ToInt64(appSettings["currentsortitem"]);}

return _currentsortitem;

}

set

{

if (_currentsortitem != value)

{
_currentsortitem = value;

appSettings["currentsortitem"] = _currentsortitem;

NotifyPropertyChanged("currentsortitem");}

}

In the xaml :

 

  <ComboBox x:Name="sortcombo" ItemsSource="{Binding sortlist}" SelectedValue="{Binding currentsortitem, Mode=TwoWay}" />

 

 

Can somebody help me on this ?

Regards,

Muthu, EMC Corporation,India

 

 

 

Jul 14, 2010 at 11:07 PM

Hi Muthu,

Based on our understanding, you need to add the same view twice and reflect the performed changes/updates in the ViewA to ViewB and conversely ViewB to ViewA by using MVVM.

The approach of binding in two way to a property in the view model seems to be correct, but this scenario can be achieved by taking into account this and also the following things:

  1. As this is a Silverlight application if you want to use the two way “binding mode”, you must implement INotifyPropertyChanged in your view model. For more information about binding you could take a look at this link on MSDN.
  2. Last but not least, you would have to share the same instance of your view model between the ViewA and ViewB, so at this point you could register a type with unity of your view model with a particular LifeTimeManager named ContainerControlledLifetimeManager. For more information about how to use Lifetime Managers you could visit this page on MSDN.

Regarding how to show the same view, I think that the better approach is to register the same view in two different regions at the same time, therefore, as a view belongs to a specific module (where it was created),  that module is the one that should register the view twice.

Please let me know if this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero