简体   繁体   中英

Dialog view containing two Caliburn.Micro views?

I have a Windows WPF app in which I'm using Caliburn.Micro. The main window view/viewmodel is handled by Caliburn.Micro. One of it's buttons pops up a new dialog window which uses a different view-viewmodel.

In this dialog I have a container (list box and some filter controls) that I want to make into a reusable control so that I can include it in other dialogs. To do this I extracted from the dialog's view and viewmodel the relevant code and created a new view and viewmodel. This all looks good.

The problem is that now back in the dialog I have a dockpanel with a big empty space where I need the reusable control to go.

From the dialog viewmodel OnInitalize() I could create the reusable control viewmodel, but I don't know how to get it's view into the dialog view's dockpanel.

To create the dialog from the main window viewmodel I use WindowManager().ShowDialog() to display the viewmodel for the dialog and Caliburn.Micro takes care of setting up the view. Is there a way I can specify in the dialog's XAML that I want to embed the view for the reusable control and have Caliburn create the appropriate view/viewmodel?

Or am I going about it the wrong way?

If you have a property on your dialog view model which is another view model type, and you add a ContentControl to your dialog view which is named the same as this property, then Caliburn.Micro will automatically inject the view which corresponds to your property view model type into the ContentControl placeholder, and bind that view model type to the view automatically too. Is this what you mean? Something like:

// Dialog View Model
private MyReusableControlViewModel myReuseableControl;
public MyReusableControlViewModel MyReuseableControl
{ 
   get { return this.myReuseableControl; }
   set { this.myReuseableControl = value;  NotifyOfPropertyChanged(...); }
}

// Dialog View Model Constructor
public DialogViewModel()
{
  this.MyReuseableControl = new MyReusableControlViewModel();
}

// Dialog View
<DockPanel>
  ...
  <ContentControl x:Name="MyReusableControl" />
</DockPanel>

Of course, ideally you would want to inject any dependencies of the dialog view model (in this case MyReusableControlViewModel) and work against abstractions inside the dialog view model, rather than concrete types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM