开发者

How To access commands on usercontrol from viewmodel

I have legacy windows forms user control that exposses several public methods. I wrapped this control on a wpf user control and encapsulated the Methods with a relaycommand on the new wpf usercontrol.

Now my problem is how to use the mvvm pattern to execute the commands on my user control form the viewmodel that is used with the开发者_Python百科 view hosting the new wpf usercontrol.


In viewmodel you have to add a field say

Public ICommand CommandOne

Now this command will create a new RelayCommand object depending upon your requirements/conditions.

Now, you can bind this 'CommandOne' command with any object say button on your control form.

So, whenever the button is clicked then the RelayCommand object will be created and it will execute the action given to it as a parameter.

Hope it works for you.


I Fond out how to get this to work with bindings. Need to set the mode to OneWayToSource to get the command from the user control. The tricky part is that the initialization of the command has to be done inside the loaded event of the usercontrol. If you try to do it inside of the constructor, you will end up with the default initialization from the binding which could be null.


Use PRISM EventAggregator? You can fireoff an event from ViewModel, from your Usercontrol subscribe to it.

http://www.codeproject.com/Articles/355473/Prism-EventAggregator-Sample https://msdn.microsoft.com/en-us/library/ff921122.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜