how to use UserControl as View in WPF with PRISM4?

Topics: Prism v4 - WPF 4
May 24, 2012 at 6:55 AM

Hi All,

             I am using PRISM4. I have many userControls. I need to create this as View.

My Shell is having TabControl. this is the MianRegion of my Project.

Currently I am creating View as TabItem like below

<TabItem x:Name="Person"

//some statndard Properties>

<Grid>

<UserControl>

</Grid>

So i registerd this TabItem in Module.

Here the Issue is I am Creating UserControl seperately, once again i need to create View as TabItem seperately.

I like to use UserControl as View Directly. How can i create this?

is it Possible?

Developer
May 24, 2012 at 2:28 PM

Hi,

Based on my understanding of your scenario, I believe you could benefit from the use of Prism regions, as this will allow you to dynamically compose your UI in a loosely coupled way.

You could find more information about this in the following chapter of the Prism documentation at MSDN:

Regards,

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

May 28, 2012 at 4:37 AM

Hi ,

         Thanks for your reply.I am Expecting some sample code.

could you give the sample code? or give bit more Explanation about how to add userControl as TabItem in a TabControl.

Developer
May 28, 2012 at 6:59 PM

Hi,

You can define the TabControl in your Shell as a region doing something like this:

<TabControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />

And then you can simply inject a view (defined as a UserControl) in the aforementioned region by using one of the UI composition methods provided by Prism. For example, if you have a UserControl named HelloWorldView you can inject it by:

// Using view injection,
var view = ServiceLocator.Current.GetInstance<HelloWorldView>();
_regionManager.Regions["MainRegion"].Add(view);

// or using view discovery.
_regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.HelloWorldView));

Prism will automatically wrap the UserControls (views) inside a TabItem (one for each view) and add them to the TabControl.

Also, I believe you might find the UI Composition labs provided in the Prism Training Kit interesting:

I hope you find this useful,

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