in

ASP.NET Weblogs

Ezequiel Jadib

There is no place like 127.0.0.1

Prism How-To: Provide metadata to a view that was placed into a region

If you downloaded our fourth drop probably you may have noticed that we have removed the IMetadataInfo and IMetadataInfoProvider classes. (See Julian's post for more info about this change).

In the Prism Forum some people are asking how to provide metadata to the view now that the IMetadataInfo class has been removed, so in this post I will explain how to do it.

Imagine that you have a TabRegion named MainRegion in your Shell and you want to add a view to it.

<TabControl Name="Tab" Prism:RegionManager.Region="MainRegion">

After you do this

regionManagerService.GetRegion("MainRegion").Add(new View())

you will see something like the following picture

Looks good, but... we don't have a header, so let's see how to do it

  • Create a HeaderModel class

    namespace PrismTabHeader { using System.Windows; public class HeaderModel : DependencyObject { public static readonly DependencyProperty HeaderInfoProperty = DependencyProperty.Register("HeaderInfo", typeof(string), typeof(HeaderModel)); public string HeaderInfo { get { return (string)GetValue(HeaderInfoProperty); } set { SetValue(HeaderInfoProperty, value); } } } }

    This class contains a HeaderInfo dependency property.
  • Update the view by adding the Model property.
    public HeaderModel Model { get { return DataContext as HeaderModel; } set { DataContext = value; } }
  • Create a new style named HeaderStyle in the Shell.

<Window.Resources> <Style TargetType="{x:Type TabItem}" x:Key="HeaderStyle"> <Setter Property="Header" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.DataContext.HeaderInfo}" /> </Style> </Window.Resources>

This style is going to be applied to TabItem elements and is saying that the value of the property Header is going to binded to Content.DataContext.HeaderInfo where Content is the view.

Note: You can also expose a HeaderInfo property in the view and change the binding path to Content.HeaderInfo.
  • Replace the TabControl definition of your Shell with the following code

<TabControl Name="Tab" Prism:RegionManager.Region="MainRegion" ItemContainerStyle="{StaticResource HeaderStyle}" />

As you may see we are setting the ItemContainerStyle dependency property to affect the appearance of the elements that contain the data items. In this case, the style is going to be applied to all TabItem elements within the scope the style is defined in.

  • Set the Model to the view before adding it to the region
    View view = new View(); view.Model = new HeaderModel() { HeaderInfo = "My Header" }; region.Add(view);

If you run now the application you will see the header.

Hope this helps.

Crossposting from here

Comments

 

Ariel Ben Horesh said:

Thanks, It was very helpful

December 20, 2008 5:55 PM
 

craig said:

thanks!  I was wondering how this might be done... helped a lot.

December 8, 2009 11:20 AM
 

raffaeu said:

One word, simply AWESOME!!

Thanks.

Have you tried to implement a solution where the tab has a close button and it triggers to the regionManager to close the corresponding view?

June 30, 2010 10:55 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About ejadib

Ezequiel Jadib is a Senior Lead Developer for Southworks, a software development company that provides consulting and development services for Fortune 50 companies around the world. He has worked for numerous projects with Microsoft, including the Smart Client Software Factory, the Web Client Software Factory, the Multi-Tenant Database Performance Guide, the Composite Application Guidance for WPF & Silverlight (Prism) and the Tech-Ed 2008 Keynote demo. He recently lead the efforts to create a set of samples and hands-on labs about the Live Services Platform, and also lead the development team for PDC 2008 demo highlighting SQL Data Platform. He is continuously contributing to the Client Application Development community sharing knowledge around Web Client, Smart Client and Composite WPF thought his blog, Codeplex and forums.