WPF MVVM: How do ViewModels communicate with each other?

后端 未结 2 1039
我寻月下人不归
我寻月下人不归 2020-12-24 03:08

I have a View which has 2 sub views on it and a ViewModel is assigned to each view:

ViewA - ViewModelA
{ 
   ViewB - ViewModelB
   ViewC - ViewModelC
}


        
相关标签:
2条回答
  • 2020-12-24 03:40

    The ViewModels usually 'inherit' relationships from the Model. And it's not wrong to add a relation when it makes sense.

    It's perfectly OK for a ViewModel to have a property referring to another ViewModel.

    0 讨论(0)
  • 2020-12-24 03:42

    One way to have disconnected ViewModels communicate to each other is to use a publish / subscribe mechanism such as PRISMs EventAggregator. However, in a parent / child ViewModel relationship, I think it's fine for the parent to have direct knowledge and control over the child ViewModel.

    Personally, I don't think composing a ViewModel out of other ViewModels is a bad practice. I do it all the time. I generally favor composition over inheritance in my ViewModels.

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