How to use Command Binding Cross Regions

Topics: Prism v4 - Silverlight 4, Prism v4 - WPF 4
Sep 7, 2011 at 7:15 AM

Dear All,

Prism is so amazing that I can' t help loving it.

Yesterday, I came across a binding problem. Here is my scenario:

I have two regions in my Shell, one menuRegion on the top and another is a TabControl called mainRegion on the bottom.

In the menuRegion, I place two buttons: Save and SaveAll.

In the mainRegion, I have  two TabItems: Tab1 and Tab2. Each of the TabItems contains a "View". These views are binding with ViewModels accordingly. Both ViewModel has a bool type property "IsDirty".

My requirement is:

1. "Save" Button can only save the content of the current active view. If current active view "IsDirty", "Save" is enabled otherwise it is disabled. When "Save" is clicked, it can use "SaveCommand" from viewmodel of active view to perform saving.

2. "SaveAll" Button can save the content of all dirty view. When any of the view "IsDirty" SaveAll is enabled otherwise it is disabled. When "SaveAll" is clicked, it can use "SaveCommand" from viewmodel of all "dirty" views to perform saving one by one.

It seems like a MDI-style window. But I have no idea how to create one using Prism (4.0). Looking forward to your advice.

Best regards


Sep 7, 2011 at 7:04 PM

Hi, I'm glad you found Prism useful.

Regarding your problem, you might try using the IsActive property declared in the IActiveAware interface for every command you use to perform saving in each view. This way you can define whenever a command becomes active. DelegateCommand might be the simplest way to achieve this.

Also you could use a CompositeCommand for the “SaveAll” button. This type of command invokes several child commands. At the same time it supports enablement. Note that by default CompositeCommand polls all its child commands by calling CanExecute on each child command. If any call to CanExecute returns false, the CompositeCommand will return false, thus disabling the invoker(s). Therefore in your scenario you will need to use the activity monitoring behavior to execute only active commands. This behavior is enabled in the CompositeCommand's constructor by setting the monitorCommandActivity to true.

You might find this thread useful, where a similar issue is addressed.

Also you'll find more information regarding that in the following links:

I hope you find this helpful.

Agustin Adami

Sep 8, 2011 at 12:53 AM

Hi, aadami

Thanks for your quick answer. I think that's the right way for me.

I will read all the reference you mentioned.