Prism Template Pack 1.4: Now with MEF Support!
Quite a few folks have asked for MEF versions of the Prism templates. Im happy to announce that the Prism Template Pack has now been updated to include MEF (as well as Unity) templates.You can download the latest version of the template pack (version 1.4) from here. You can read the release notes here.
Remember to remove the .zip extension (leaving the .vsix extension!) before double-clicking to install the templates.

The Prism Template Pack now provides templates for a quick-start solution, a shell and a module, for both Silverlight and WPF, and in C# and Visual Basic.NET. Thats a grand total of 24 individual templates (!!), which is one reason why it took so long to release this update.
To make it easier to find the right template, Ive added MEF and Unity categories under the Prism node. This approach isnt ideal but it means you wont have to parse a large flat list of templates. In the near future Ill investigate moving to a wizard-based approach where you can choose between MEF and Unity, and other configuration options, using a wizard dialog. That should allow the templates to support more variations without exponentially increasing the number of templates
.
As always, please let me know what you think.

Closable Tabbed Views in Prism

Prism regions make it easy to change the layout of views within an application. A region is a logical placeholder associated with a specific layout control. Displaying a view in a region causes the view to be added to the layout control. But because the region and the layout control are loosely coupled, you can easily swap the layout control for a different one without having to change the modules in the application. For example, you can change from a tabbed view to a single document view.
While Prism makes it easy to add views to regions, it isnt obvious how to close and remove a view from a region, especially from the UI. A common scenario is when you have multiple views displayed in tab control and the user can close individual views using a close button on the tab header. You can see this tabbed view style of UI in Internet Explorer and in Visual Studio.
This sample shows how this can be done. Its based on the quick start solution thats provided by the Prism 4.0 Template Pack. There are really only a couple of minor changes to the quick start solutions shell. All of the real work is done in a new re-usable Blend action class called CloseTabbedViewAction.
The Shell View
Before we dig into that class, lets take a look at the changes in the shells view. The TabControl that is used to layout views in the Main region is declared as:
1: <sdk:TabControl prism:RegionManager.RegionName="MainRegion" ...
2: prism:TabControlRegionAdapter.ItemContainerStyle=
3: "{StaticResource TabHeaderStyle}">
4: ...
5: </sdk:TabControl>
!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->
with the item container style defined as:
1: <Style x:Key="TabHeaderStyle" TargetType="sdk:TabItem">
2: <Setter Property="HeaderTemplate">
3: <Setter.Value>
4: <DataTemplate>
5: <StackPanel Orientation="Horizontal">
6: <Image ... />
7: <TextBlock ... />
8: <Button Content="x" ToolTipService.ToolTip="Close this view." ...>
9: <ei:Interaction.Triggers>
10: <ei:EventTrigger EventName="Click">
11: <local:CloseTabbedViewAction />
12: </ei:EventTrigger>
13: </ei:Interaction.Triggers>
14: </Button>
15: </StackPanel>
16: </DataTemplate>
17: </Setter.Value>
18: </Setter>
19: </Style>
!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->
The tab header styles defines the header template for each tab item. This template defines a button thats simply triggers the CloseTabbedViewAction on line 12. Thats all the changes we need in the shell. This approach allows us to keep the logic behind this action nicely encapsulated.
The CloseTabbedViewAction Class
When the user invokes the CloseTabbedView action, we just want the view to be removed from the region. The Prism Region class defines a Remove method for just this situation. Once we call the Remove method, the associated TabControl will be automatically updated and the TabItem thats being used to display the view will be cleanly removed. Pretty easy right? Well, unfortunately its not quite that easy
The Remove method takes a reference to the view to be removed. So in the Invoke method of the action class we need to somehow get a reference to the view and to the region thats (logically) hosting it.
The parameter to the Invoke method provides a reference to the element that triggered the action (in this case the button defined as part of the tab header template). This element is available through the OriginalSource property. From there we can get a reference to the parent TabItem and TabControl by traversing up the visual tree. The CloseTabbedViewAction class defines a helper method called FindVisualParent to help us do this: