Does Caliburn.Micro play nicely with user controls?
Asked Answered
I

1

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?

Initiate answered 6/3, 2012 at 8:39 Comment(0)
B
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>
Bomber answered 6/3, 2012 at 11:14 Comment(7)
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? thxvmNepean
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.Bomber
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.Nepean
Yes that's correct, or you could use a Window if you were using the window manager and wanted to control the window properties declarativelyBomber
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.Nepean
Have a look at msdn.microsoft.com/en-gb/library/system.windows.window.aspx and mindscapehq.com/blog/index.php/2012/03/13/…Bomber
Can also use the DI container and include MenuViewModel as a ctor arg in ParentViewModel to automatically wire them up.Dwyer

© 2022 - 2024 — McMap. All rights reserved.