Passing unique value for each module at runtime to properties on the MainViewModel.

Topics: Prism v4 - WPF 4
Aug 21, 2012 at 6:50 PM

Hello,

I have a shell which has a few items on there that I set for each module, such as screen title, some color changes, I want to be able to set those values from the module itself. 

Let's say for example, I have a border and if I go to Module A, I want that brush to be RED (for example), and if I go to Module B, I want the brush to be YELLOW.  

I have properties ann the MainViewModel, but how can I set those at run time from each module on the main shell.

Aug 21, 2012 at 7:39 PM

I tried to use the RegionContext to pass a single property, I will need to pass a complex object eventually. 

My Region in xaml looks like

<ContentControl Prism:RegionManager.RegionName="{x:Static regions:RegionNames.MainRegion}"
Prism:RegionManager.RegionContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.WindowTitle, Mode=TwoWay}"
In my Constructor for the view model, I tried both, but the value still comes back null period.
var xxx = regionManager.Regions[RegionNames.MainRegion].Context;
var xx = RegionContext.GetObservableContext(this).Value;

Hope this helps in giving me an answer on what I am doing wrong.

 

Developer
Aug 21, 2012 at 8:09 PM

Hi,

Based on my understanding of your scenario, I believe you could take advantage of the communication capabilities provided by Prism. In my opinion, using the RegionContext doesn't seem to be a suitable approach for this scenario (as the RegionContext is designed to be used as a communication line between views contained in the same region.) Instead, I believe you could find the EventAggregator or a Shared Service more useful.

For example, your Shell's view model could subscribe to an event in the EventAggregator to know when the corresponding properties (like for example the Brush of a Border) should be changed. The new value could be included in the object passed as a payload of the event. Then, when those properties need to be changed (for example, when you navigate to a view of the "Module A") you can publish that event through the EventAggregator (for example, in the NavigatedTo method of the view model of the aforementioned view) to notify the Shell that the property should be changed.

You can find more information about the EventAggregator and Shared Services in the following chapter of the Prism documentation:

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini