Composing a screen out of multiple usercontrols

Topics: Conventions, Getting Started, UI Architecture
Feb 15, 2012 at 2:03 PM


I can not seem to find how Caliburn supports my preferred way of working with screens consisting of (nested) usercontrols.

The following situation provides a problem for me

- I have a clean shellview
- I have defined a usercontrol "MyView" It has a button with x:Name="DoStuff".
- I have also defined a viewmodel  "MyViewModel" with a method public void  DoStuff
- When I include the usercontrol into the shellview I get an exception when pressing my button.

As I understand Caliburn.micro is looking for the DoStuff method on my ShellViewModel class, not on my MyViewModel class.

After experiencing this I googled and found this thread ( where I learned that traversing usercontrols would create too much of a UI tree traversal and was not done for performance reasons. 

I read through the "Screens, Conductors and Composition" in the documentation but as far as I can see this does not provide me with a solution for creating screens which consist of usercontrols which may be used in more than one screen. All it seems to refer to is how a screen would interact with other screens. The composition of a single screen is not really explained.

I've also read about the view-first way where you set the associated viewmodel in the xaml of your view.
It is however stated that the view-first way of working is not the default and to me it sounds like it is a workaround for solve my issue.

What is the caliburn way of working when it comes to using usercontrols in your screens?

Feb 15, 2012 at 2:57 PM

solved it, found the following solution:

you need to create and instantiante a MyViewModel property on my ShellViewModel class.

Furthermore you need to bind the usercontrol in xaml to this property 

<Views:MyView cal:Bind.Model="{Binding MyViewModel}"></Views:MyView>

Jan 27, 2014 at 2:59 AM
I think that a better solution might be the one shown at:

This solution consists of:

<customControls:CompanyNameControl cal:Bind.Model="{Binding}"/>

In this solution there is mention of the view model which is something that the view should know nothing about.
Jan 28, 2014 at 5:43 AM
Sorry for the type on my previous post. It should say "In this solution is NO mention of the view model..."