Tabcontrol as region : TabItem selectionChanged ?

Topics: Prism v4 - Silverlight 4
Mar 29, 2011 at 12:52 PM

Hello,

if I have a TabControl like this as a region, where I can add new Views in there, how do I know when a different tab was clicked by the user? Since I am adding a View to each tab of the region above like this:

var contactViewModel = _container.Resolve(); 
var view = ContactView(contactViewModel);
_regionManager.Regions[Constants.MainRegion].Add(view); 



I was thinking I might be able to utilize the SelectionChanged event like this, in order to delegate it to the Command inside the ContactViewModel;

<sdk:TabControl regions:RegionManager.RegionName="MainRegion"/>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
      <i:InvokeCommandAction Command="{Binding TabSelectionChangedCommand}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</sdk:TabControl> 

However this would only work within Shell's ViewModel, and is a bit useless. Then I was thinking about using the GotFocus Event on the actual TabItem.

<sdk:TabControl regions:RegionManager.RegionName="MainRegion"/>
   <regions:TabControlRegionAdapter.ItemContainerStyle>
     <Style TargetType="sdk:TabItem">
     ...
     </Style>
  </regions:TabControlRegionAdapter.ItemContainerStyle>
</sdk:TabControl>

But since there is no EventSetter in SIlverlight, how would I set the GotFocus Event of the TabItem to a Command by utilizing the Interaction.trigger as above?

Many Thanks,

Houman

Mar 29, 2011 at 3:41 PM

Hi Houman,

One possible way to achieve your requirement could be to subscribe to the CollectionChanged event in the Region´s ActiveViews. By doing so, when a view is activated by switching each tab, you will be notified in the event handler you’ve specified. This way, you could have access to this notification from any component in your composite application, since the RegionManager is available throughout your application.

Other possible path to take is to make use of Globally Available Commands. You can find more information about Global Commands in the sections titled “Making a Command Globally Available” in Chapter 9: Communicating Between Loosely Coupled Components.

I hope you find this information usefull.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Mar 30, 2011 at 12:15 PM

Miguel,

 

Thank you so much for the tip with the CollectionChanged of the ActiveViews. It works wonderfully. :)

 

Regards,

Houman

Sep 5, 2011 at 6:36 AM
mbronzovic wrote:

Hi Houman,

One possible way to achieve your requirement could be to subscribe to the CollectionChanged event in the Region´s ActiveViews. By doing so, when a view is activated by switching each tab, you will be notified in the event handler you’ve specified. This way, you could have access to this notification from any component in your composite application, since the RegionManager is available throughout your application.


Tanks Miguel, that is the most logical and "best code practices" way of doing it

Can you explain how to do it

I can't find it here

http://msdn.microsoft.com/en-us/library/gg430865%28v=PandP.40%29.aspx

Saludos

Developer
Sep 5, 2011 at 5:38 PM

Hi,

A way to achieve what Miguel mentioned would be something like this:

_regionManager.Regions["MyRegion"].ActiveViews.CollectionChanged += Regions_CollectionChanged;

And then, in your handler define your custom logic:

void Regions_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)

     {

      //Insert the custom logic for your scenario

     }

This way you will be notified in the event handler when a different view is activated.

Please let us know if this isn't enough for you to understand how to achieve this.

I hope you find this helpful.

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

Sep 5, 2011 at 10:25 PM

Thank you Agustin

I am really new to Prism

do you think you could help me here on the following thread

http://compositewpf.codeplex.com/discussions/271552

Regards