Caliburn.Micro getting it to bind UserControls in a MainView to their ViewModels

后端 未结 1 1766
南方客
南方客 2021-02-04 07:23

I have a MainView.xaml, binding to a MainViewModel just fine.

What I wanted to try out was splitting a lot of controls I have on my main form into UserControls.

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-04 08:00

    You don't need to use a Conductor here. That's basically used for navigation scenarios. Just make two public properties on your MainViewModel one for RightSideControlViewModel called RightSide and one for LeftSideControlViewModel, called LeftSide. Then, instead of instantiating your UserControls directly in your MainView, create two ContentControls, one with x:Name="LeftSide" and the other with x:name="RightSide" This is a view-model-first way of accomplishing it. If you want to do it view-first, keep the user control definitions in your MainView, but change the Bind.Model so that it points to the new properties you created, like Bind.Model="{Binding LeftSide}"

    Basically, the way you have things defines....the bindings just aren't pointing at the right objects, more or less. You've got the Conductor in there, which you don't need to accomplish this. You may want to keep it if you intend to have some sort of navigation architecture. Remember that when you call ActivateItem on a Conductor, you are basically changing its ActiveItem property; only one model with be active at a time. In the case above you activate both items, but only the second one remains active. Furthermore, in your view, nothing is bound to ActiveItem.

    I hope this makes sense!

    0 讨论(0)
提交回复
热议问题