IActiveAware question.

Topics: Prism v4 - WPF 4
May 17, 2012 at 1:39 AM
Edited May 17, 2012 at 1:41 AM

I am trying to design a pattern using Prism, MEF, and the composite model to add a dynamic toolbar. I have a TabControl region that "houses" the main application tabs. The global toolbar will have its views injected into a global toolbar region defined in the Shell. Since I need to add/remove views dynamically, I read up on the IActiveAware interface and view injection and I am close to my solution. However, when I have my View implement IActiveAware the IsActive event fires as expected; when the user clicks on the tab, the event fires, when the user clicks on some other tab, the event fires indicating IsActive equals false. But, if I move the IActiveAware implementation to the ViewModel (in keeping with a bare-bones code-behind file suggested by P&P) the IsActive event fires once on startup with no change when the user clicks on the tabs.

I thought about using a command that notifies the ViewModel when the View gets an IsActiveChanged event, or using the EventAggregator in a similar fashion - but this seems like a bad way of doing things.

Is this the normal behavior for ViewModels and the IActiveAware interface, shouldn't the View and ViewModel act similarly when one or the other implements this interface?

Any feedback would be very welcomed!

Developer
May 17, 2012 at 8:28 PM

Hi,

Based on my understanding, if the view model of your view implements the IActiveAware interface, then the IsActive property in the view model should be set to the corresponding value automatically thanks to the RegionActiveAwareBehavior. However, take into account that the IsActiveChanged event won't be raised automatically and it must be raised manually in your view model, like for example, in the setter of the IsActive property.

If, for example, the IsActive property of your view model is not updated automatically or you are experiencing another problem regarding the implementation of the IActiveAware interface, it would be helpful if you could provide us with more information about this (like for example, a code snippet of your view / view model) or a repro-sample application portraying this problem so that we can help you further with this.

I hope you find this useful,

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

May 17, 2012 at 9:39 PM

I was trying to figure out why the IsActive property gets set when the View implements IActiveAware (and when the corresponding view has "focus" or not). But not when the ViewModel implements the interface.

I have a tab region with several TabViews (that get injected into the tab region) and each tab view contains multiple child views that are displayed when a user clicks on a particular tab. To enable a toolbar that reflects actions a user can perform depending on which TabView is "active", I inject views into the Shell toolbar region (which is seperate from the tab region but both are defined in the Shell.xaml file).

I was just wondering why if a View implements IActiveAware (and not the ViewModel), it's IsActive property gets set when the tab is visible or not (when user clicks on other tabs - moving away from current tab), but if the ViewModel implements IActiveAware (and not the View), IsActive fires only once and moving amongst the tabs has no effect on setting IsActive.

I know I'm missing seomthing blatantly obvious, but I'm still a bit muddy on this.

Thanks.

Developer
May 18, 2012 at 2:04 PM
Edited May 18, 2012 at 2:05 PM

Hi,

I believe you could check the following thread, which contains a sample application that portrays the use of the IActiveAware interface in a View Model, although the views are injected in a ListBox control this can be directly replaced with a TabControl:

Also take into account that when applying this interface in your View Model you will have to notify the UI that the IsActive property in the View Model changed. This can be achieved by calling the RaisePropertyChanged("IsActive") method in the setter of your IsActive property inside your View Model.

I hope you find this useful,

Agustin Adami
http://blogs.southworks.net/aadami