I have a DomainView that allows you to select an Entity in my domain. The Entity is displayed in an EntityView within the DomainView.
My question is what should the 'DomainViewModel' pr开发者_如何学Coperty be that the EntityView binds to?
- The Entity, with the View itself wrapping it up in a EntityViewModel and it binds to that?
- The Entity, using a Converter on the binding to convert between the Entity and EntityViewModel?
- An EntityViewModel, created by the DomainViewModel?
All would work, I just wondered what would be the 'MVVM-way'? My preference would be for one of the last two.
Lee
With the typical 'MVVM-way', ViewModels shouldn't be aware of other ViewModels and the relationship between View and ViewModel is 1-1.
Sounds like your real question is "How do I communicate data between ViewModels"? A common Master/Details interaction.
Are you using any frameworks? I am personally more familiar with PRISM, but the concepts are similar in MVVM Light and others. In PRISM, a good solution is the EventAggregator
. The DomainViewModel publishes an "EntitySelected" aggregate event that is subscribed to by the EntityViewModel.
Another option is to inject a common service (or model, depending on your style) into both ViewModels. That service would provide a public property like CurrentEntity that is set by the DomainViewModel as needed.
Either would provide a mechanism of communicating data between the ViewModels without those ViewModels having any dependency on each other.
精彩评论