Binding two UserControls to the same DataContext, or ViewModel?

前端 未结 1 862
闹比i
闹比i 2021-01-14 17:55

I am working on a UserControl, which is composed of a Chart panel and another area which manipulates some of the chart data i.e. the chart controls

相关标签:
1条回答
  • 2021-01-14 18:15

    You absolutely can have your two UserControls bind to the same ViewModel. You can either bind the DataContext of each to the same object instance, or I suppose you could have the DataContext of one control bound to the DataContext of the other and have it bound to your ViewModel.

    If you're starting with a working implementation of your view and your viewmodel, and you feel like the XAML is getting unwieldy, I would agree that you should separate the XAML into multiple views. There's no reason that means you need to separate your ViewModel implementation until you feel that it is becoming incoherent, or just too big. Separating it now would just add complexity as you suggested that they would need to communicate anyway.

    You might consider starting by simply creating a UserControl out of the chart controls area and embed that control inside your existing control. Then you don't have to modify any screens that use your chart control.

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