16

I'm a novice WPF programmer. I'm trying to add some structure to my code: both User Controls and MVVM.

Researching here, I've found that people recommend Caliburn.Micro. On the other hand, I've found some complaints here and elsewhere about Caliburn.Micro not playing nicely with UserControls.

So my question is: Does Caliburn.Micro play nicely with User Controls?

Community
  • 1
  • 1
Avi
  • 15,696
  • 9
  • 39
  • 54

1 Answers1

38

Yes, Caliburn.Micro plays nicely with user controls. It's an opinionated framework, but not to the point of forcing you down a particular development path. As the answers to the linked questions suggest, you can always use plain old WPF binding if you have any particular issues.

In fact, I wouldn't let those two links deter you at all, the first is describing a way of binding separate properties to a single user control, and the solution is valid. A better solution would probably be to use an ItemsControl with a custom DataTemplate, and then create a collection of DTOs on his view model which contain the property names and values.

The second link is stating how if you create a view (UserControl) and create an instance of the view in XAML, and you wish to bind it to a view model, then that is called view first, and you have to tell Caliburn.Micro where the view model is to bind to:

<UserControl ...
   cal:Bind.Model="EasyPlayer.MediaControl.NowPlayingViewModel" />

So, this conceptually can be thought of as a viewmodel/view rather than a UserControl with dependency properties etc.

In fact, you'll find when you use Caliburn.Micro, you'll probably use less and less UserControls to perform view composition. This is because it is very easy to create reusable pieces of UI using view models, views, and the view model first approach.

When you have a ContentControl in a view with the same name as a view model property on your parent view model, then Caliburn.Micro will locate the view of the corresponding view model, inject it into the ContentControl, and bind up the view/view model.

For example:

public class MyParentViewModel : Screen
{
  public MenuViewModel MenuViewModel { get; set; }

  public DetailsViewModel DetailsViewModel { get; set; }

  public MyParentViewModel()
  {
    this.MenuViewModel = new MenuViewModel();
    this.DetailsViewModel = new DetailsViewModel();
  }
}

<Grid> 
  <Grid.ColumnDefinitions> 
    <ColumnDefinition Width=".2*" />
    <ColumnDefinition Width=".8*" />
  </Grid.ColumnDefinitions>

  <ContentControl Grid.Column="0" x:Name="MenuViewModel" />
  <ContentControl Grid.Column="1" x:Name="DetailsViewModel" />

</Grid>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
devdigital
  • 34,151
  • 9
  • 98
  • 120
  • starting out with caliburn and just came across your very helpful post - ... could expand a bit on now using usercontrols as much ... afaics a view is going to be a usercontrol isn't it? thxvm – Simon Woods Jan 30 '13 at 16:22
  • 2
    Yes, with Caliburn.Micro you won't use user controls in the sense of user controls with dependency properties, instantiating the controls directly from another user control/window etc. – devdigital Jan 30 '13 at 21:15
  • ok ... but you'll still house, say, a grid in a usercontrol (with all the custom behaviour in the viewmodel/xaml) and get that dynamically loaded into a ContentControl via Caliburn.Micro? (Just want to check I'm not missing something). Thx very much again. – Simon Woods Jan 31 '13 at 06:32
  • 2
    Yes that's correct, or you could use a Window if you were using the window manager and wanted to control the window properties declaratively – devdigital Jan 31 '13 at 08:15
  • Excellent. Thx very much. Sorry, but as I say new to this. Could you explain (or perhaps point to a link to explain) a bit more about what you mean wrt using windows? Thx again. – Simon Woods Jan 31 '13 at 09:16
  • 2
    Have a look at http://msdn.microsoft.com/en-gb/library/system.windows.window.aspx and http://www.mindscapehq.com/blog/index.php/2012/03/13/caliburn-micro-part-5-the-window-manager/ – devdigital Jan 31 '13 at 09:54
  • Can also use the DI container and include `MenuViewModel` as a ctor arg in `ParentViewModel` to automatically wire them up. – mxmissile Aug 10 '17 at 16:37