Adding UserControls to Region

Topics: Prism v4 - WPF 4
May 26, 2012 at 9:12 PM

Hi,

I'm pretty new to MVVM / Prism and was wondering if someone would be able to tell me whether I am going about this the right way.

I have a user interface that has left hand side and right hand side regions. The user will make a selection in the left hand side region and then the selection's data will be retrieved and displayed in the right hand side region using a custom user control.

I have the modules for each of the two regions getting loaded correctly and events passing between them using the event aggregator. My question is regarding the population of the user controls in the right hand side region.

At the moment I am contructing the controls and adding them to an ObservableCollection which is bound to an ItemsControl. To clarify, here is some code illustrating what I'm doing when the right hand region receives a selection.

private void OnAddObjectA(ObjectA objA)
{
    ObjectAViewModel objModel = new ObjectAViewModel(eventAggregator, objA);
    ObjectAView objView = new ObjectAView(objModel);

    objectAs.Add(objView);
}

I essentially want to know whether there is a better way of doing this or is my method acceptable within the design pattern?

Sorry if anything is unclear, as I said I'm new to this and not completely sure about the right terminology for everything!

Thanks,

Developer
May 28, 2012 at 3:02 PM

Hi,

There are several ways to achieve this kind of scenarios, which approach you take may depend mostly on your personal preferences and your requirements.

As a suggested approach, I recommend you to check the UI Composition QuickStart provided with Prism.

Also, you could find the samples provided in the Prism Training Kit useful (particularly you could check the Communication Lab samples).

I hope you find this handy,

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

May 28, 2012 at 3:52 PM

Thanks for the links.

Will take a look through them and see how I get on.

Cheers,

May 31, 2012 at 6:26 PM

Hi,

I've looked at the links you sent and they were very helpful.

I have now setup my project with a Main Region Controller that adds views to the region when commanded.

However, this leads me on to my next issue with nested views. Each View (Object A) added will have another ItemsControl beneath it with a variable amount of user controls (Object B) within it. And each 'Object B' will contain another level of controls (Object C).

What is the best way to handle these nested levels? Should my Main Controller class be inserting the Object B's into my Object A or should this be handled by the Object A View Model.

Should I have another region within the Object A control to store the Object Bs? If so, how will I distinguish which Object Bs belong to which Object As if I want to retrieve them from the Controller or another location.

If this scenario isn't clear let me know, I will try to clarify as best I can.

Thanks again,

Developer
Jun 1, 2012 at 8:20 PM

Hi,

In my opinion, which approach you should follow will depend mostly of your personal preferences, the requirements of your scenario and the functionality of the "Objects" you mention.

As an example, suppose that the aforementioned objects are defined like this:

  • Object A: A view showing a list of clients and actions to interact with the list (ordering, filtering, reloading, etc.)
  • Object B: A view that shows information of a specific client and that is contained in the Object A's client list (one Object B for each client.)

If the Object B must only show some information about the client, Object B could be simply an ItemTemplate for the Object A's ItemsControl (the client list) and the Source of that ItemControl would simply be a collection of client models. In this case, due to the "simplicity" of the Object B, there was no need to define a separate view and view model for each client.

On the other hand, if the Object B will also provide the user with options like modifying the client's data or more complex operations, it seems reasonable to define Object B as a separate view with its own view model to contain the data and handle such operations. In this case, the Object A's client list could be a scoped region where the Object B views or view models could be injected. The Object A's view model or an specific controller for the Object A could then inject the required instances of Object B in the scoped region. Object B could also contain scoped regions and so on...

Regarding how to retrieve an specific Object B, you could take benefit for the different communication approaches provided with Prism as explained in the following chapter of the documentation:

Also, you can find more information about scoped regions and scoped region managers in the following chapter of the Prism documentation:

As a side note regarding the use of scoped regions and scoped region managers, I believe you might find the RegionManagerAwareBehavior and the IRegionManagerAware interface described in the following blog post interesting:

I hope you find this useful,

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

Jun 6, 2012 at 10:40 AM

Thanks for the info. This sounds like what I was looking for.

Cheers,