WPF: Calling method in View from viewModel
I'm using AvalonDock in a WPF application, and need to persist the layout of the AvalonDock the user has setup (moving, docking, detaching of the panels).
The function to do that are on the control itself (SaveLayout, RestoreLayout).
What is the correct way to do that ?
Right now I have a command at the mainWindowViewModel that gets created by the application when creating the i开发者_开发知识库nstance of the window view and the viewmodel. It basically makes the relay command call an anonymous method that calls the needed function on the view control. This works since the application creates the window and the viewmodel for it.
But how would i approach this if some lower level view and viewmodel had to do this? If using this method I'd have to still create the Command at application level and sending it through the viewModels down to where it is needed to be bound to? Inside it I'd have to search the usercontrol that is a view then the avalonDock control inside it and use that at application level, which is bug prone. Somehow it seems dirty...
Thanks!
You might introduce an interface IView so that the ViewModel can call a method on the View. This way the ViewModel doesn’t need to know about the concrete View class and can still be unit tested.
How this can be accomplished is shown by the sample applications of the WPF Application Framework (WAF).
You can use decoupled messaging to communicate between view models
http://www.bradcunningham.net/2009/11/decoupled-viewmodel-messaging-part-1.html
精彩评论