MenuBarItems in MVVM

独自空忆成欢 提交于 2019-12-12 02:19:01

问题


I am playing with mvvm and wpf. Now, my total solution is MVVM-friendly. The only thing i have put in code behind is the "make new product" & close buttons on the mainview.

Now im adding a menubar, and i was wondering if i can put these "make new product" & close Items in code behind, or is this a no go?

Thanks in advance.


回答1:


The MVVM way to do it is commands. You can consider them as proxies between your declarative XAML and imperative VM.

  1. Create CreateNewProductCommand, implementing ICommand.
  2. Create a handler for the command performing the actual work as part of ICommand interface implementation (conventionally called On*** - OnCreateNewProductCommand) (you may want to pass paramteres for edit, which is supported by the interface too).
  3. Expose your command as property of your VM.
  4. Bind your menu item command (it'll likely have it, just search for properties containing Command) property to that command using standard binding syntax pointing to the relevant property created at a previous step.

This is not the only way to do it. There're more advanced techniques based on interactions/behaviors etc. Some of them would allow you to bypass command creation and bind your UI element event directly to the executable member of your VM.



来源:https://stackoverflow.com/questions/22690615/menubaritems-in-mvvm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!