silverlightsilverlight-4.0mvvmcaliburn.microcontentcontrol

Two dynamically assigned ContentControls in single view in Caliburn.Micro


I have a UserControl that contains two ContentControls that need to have different UserControl Views bound to them at runtime. The attached-Property solution noted here does not seem to work in Silverlight. Or, I am doing something wrong. I also, found this, but it did not bring any joy either.

I had a single ContentControl working by naming it 'ActiveItem'. But, of course, I cannot have two ContentControls with the same name.

Thanks in advance for any help,

Jim


Solution

  • Just expose two public properties on your main view model, each one being an instance of the type of view model you wish to display. Then, in your view have a ContentControl with the corresponding name. E.g:

    public class MyMainViewModel
    {
      private NavigationViewModel navigation;
      private MyContentViewModel main;
    
      public MyMainViewModel()
      {
        // better to inject factories using constructor injection here
        this.Navigation = new NavigationViewModel();
        this.Main = new MyContentViewModel();
      }
    
      public NavigationViewModel Navigation
      {
        get { return navigation; }
        set { navigation= value; NotifyOfPropertyChanged(() => this.Navigation); }
      }
    
      public MyContentViewModel Main
      {
        get { return main; }
        set { main= value; NotifyOfPropertyChanged(() => this.Main); }
      }
    
      ...
    }
    
    <ContentControl x:Name="Navigation" />
    ...
    <ContentControl x:Name="Main" />