开发者

Caliburn Micro: "children" VM in a main shell VM

开发者 https://www.devze.com 2023-03-10 13:02 出处:网络
I\'m beginning with Caliburn.micro and I\'m a bit confused. Say I have a U开发者_JAVA技巧I with 2 panes, e.g. (this is a fake sample) CustomersView and CustomerView, and 2 corresponding VM\'s, Custome

I'm beginning with Caliburn.micro and I'm a bit confused. Say I have a U开发者_JAVA技巧I with 2 panes, e.g. (this is a fake sample) CustomersView and CustomerView, and 2 corresponding VM's, CustomersViewModel and CustomerViewModel.

Now, say I want to include both panes into a main shell, which should be able to access all the VM's like data members: e.g.

public class MainViewModel
{
  private CustomerViewModel _vmCustomer;
  private CustomersViewModel _vmCustomers;
  ...
}

As viewmodels are created by CM, how can I connect my main shell to each instance of them? Or is this a wrong approach? I do not need a conductor in its canonical sense here, as I'm not activating or deactivating a set of panes like in a MDI: I have a SDI UI with some panes, each backed by its VM, and a main shell which should manipulate all them. What is the right approach in a similar scenario?


In your MainView.xaml add two ContentControls and give them names that match the names of two properties representing your ViewModels.

ViewModel:

public CustomerViewModel CustomerViewModel { get; set; }
public CustomersViewModel CustomersViewModel { get; set; }

View:

<ContentControl x:Name="CustomerViewModel" />
<ContentControl x:Name="CustomersViewModel" />


Another way to do it is

public CustomerViewModel Customer { get; set; }

and

<myApp:CustomerView cal:View.Model="{Binding Customer}/>

This gives a reader a better idea what to expect from the view. Blendability could be better, too.

0

精彩评论

暂无评论...
验证码 换一张
取 消