WPF MVVM: How do ViewModels communicate with each other?

后端 未结 2 1041
我寻月下人不归
我寻月下人不归 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: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.

提交回复
热议问题