I am doing a silverlight using the MVVM model, and i am finding it hard to do the events handling via MVVM especially that the events handlers are doing lots of changes in the view like enabling and disabling buttons, update media element functions and position. I am still new to the MVVM and i can't Imagen how can i do this. does anyone already know good article to start with or simple approach to understand :) I'll reply with what i may find interesting while i do my search as well. Thanks
+2
A:
1) Understand that there are different "flavors" of MVVM. Strict/hardcore MVVM patterns, although theoretically desires, isn't necessary.
2) Many view events can be handled via Commands. WPF supports this, and i believe Silverlight 4 does as well. A simple view-event to start with would be Button clicks. This allows you to handle the event in the ViewModel (instead of the View's 'code-behind').
3) For things like enabling/disabling view controls/states via the MVVM model, here is an example/explanation:
- Xaml controls (say, a Button) is Data-Bound to the ViewModel for whatever property (in this case, it will be the button's IsEnabled property).
- Your ViewModel has an IsButtonEnabled property.
- Whenever you change this property in the VM, raise the PropertyChanged notification, and you will see the binded result in the view (the button's IsEnabled state will be updated).
ps - you can do many things via VM properties in this manner: from text, to various property states, color, you can even play animations in the property setters/getters....etc.
Cheers
AlvinfromDiaspar
2010-07-30 21:21:52
Thanx for explanation, have a question. is it ok to send a control as a parameter and do whatever i want in the VM. Is that a bad practice ?
Miroo
2010-07-30 21:32:23
At least in WPF on Windows, if you are binding to commands, you don't have to have to bind to an additional "IsXxxEnabled" variable. Just make it so the result changes in the command's CanExecute method, and raise the CanExecuteChanged event.
Merlyn Morgan-Graham
2010-07-30 21:34:42