开发者

Reuse controls inside a usercontrol

I have a UserControl UserControl1 and a button inside the UserCo开发者_StackOverflow中文版ntrol1. And I have a UserControl1ViewModel that has an ICommand property for the button. Using this command I need to call a method outside(from other VMs or VM of the MainWindow) the VM. What is the best practice for this?


You might want to examine MVVM lite by Laurent Bugnion http://www.galasoft.ch/mvvm/getstarted/

This is a lightweight toolkit for helping enforce mvvm concepts. In it, every viewmodel is a static member in a ViewModelLocator class. So for instance, in your command you could do something like this.

ViewModelLocator.MainViewModel.MainContent = NewContent;

You can totally do this without mvvm lite, but using it really helps speed up the learning curve and enforce modularity.


You're most likely looking to implement the Mediator pattern to handle the communication between two viewmodels.

Another SO question along the same vein is: mvvm-view-model-view-model-communications


I would consider using Controllers for the mediation between the ViewModels. The WPF Application Framework (WAF) shows how this works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜