Dialog view containing two Caliburn.Micro views?

百般思念 提交于 2019-12-07 14:22:51

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4412677/dialog-view-containing-two-caliburn-micro-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!