Using EventAggregator to add TabItems dynamically in prism

Topics: Prism v4 - WPF 4
Jan 16, 2013 at 7:53 PM
 

I need to add TabItems dynamically at run time to TabControl, I found some information in the web but it is very brief and i need some detailed steps. thanks in advance

Developer
Jan 16, 2013 at 8:33 PM

Hi,

I am not aware of the requirements of your scenario or why you want to compose the UI through the use of the EventAggregator, but based on my understanding, in Prism the dynamic composition of the UI is usually done through Regions. For example, you could define your TabControl as a Region doing something like this:

<TabControl cal:RegionManager.RegionName="TabRegion" />

Then, in any part of your application, you can dynamically add a TabItem to the TabControl using one of the UI composition approaches of Prism, for example:

this._regionManager.Regions["TabRegion"].Add( tabItem );

You can find more information about Regions in the following chapter of the Prism documentation:

 

On the other hand, if you need to involve the EventAggregator in the process of composing the UI, you might find the following thread interesting:

At the end of the aforementioned thread, you can find a sample application that portrays the usage of events to change the UI at run-time. This sample uses a ContentControl, but could be adapted for a TabControl easily.

I hope this helps,

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

Jan 17, 2013 at 5:57 AM

Hi,

i have an event that is triggered from view in Module_A that should cause another view in same module or another Module_B to be displayed on TaobControl on the Shell Window in Module_C in a new TabItem. So i cannot do this simply like you explained.

Developer
Jan 17, 2013 at 1:49 PM

Hi,

In my opinion there is not only one way to achieve this kind of scenarios and which approach you take will depend mostly on your personal preferences and the requirements of your scenario, for example as a possible approach you could use both EventAggregation and regions to achieve this, this way you could subscribe to the triggered event in Module_A, either in same Module_A or Module_B depending on your needs, and then in each of the handlers for this event, you could use one of the UI composition approaches like the one mentioned above, in order to add the corresponding view in each of these modules to the region defined in the TabControl in Module_C.

Or for example, I believe another possibility could be: in the handler of the event triggered from view in Module_A you could inject the required view to the region defined in the TabControl in Module_C, but in order to inject the correct view, for example in this unique handler you could have some logic to decide which view will be injected in this region. For example if you need to display the view that is contained in Module_B, you could export this view to the corresponding dependency injection container in this module by using an specific contract, in order to allow you to retrieve it in the Module_A event handler through the container if needed.

On the other hand, if your concern is related on how to publish and subscribe to events using the EventAggregator, I believe you could find the Event Aggregation section handy, as it portrays several code snippets and detailed information on how to implement this communication approach.

Please let us know if this makes sense for your scenario,

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