开发者

what's the best practice for events handling in MVVM

开发者 https://www.devze.com 2023-01-09 21:03 出处:网络
I am doing a silverlight using the MVVM model, and i am findin开发者_运维知识库g it hard to do the events handling via MVVM especially that the events handlers are doing lots of changes in the view li

I am doing a silverlight using the MVVM model, and i am findin开发者_运维知识库g 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


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

0

精彩评论

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