MVVM中的MenuBarItems [英] MenuBarItems in MVVM

查看:86
本文介绍了MVVM中的MenuBarItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩mvvm和wpf.现在,我的总体解决方案对MVVM友好.我在代码中留下的唯一内容是制作新产品"和关闭主视图上的按钮.

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?

先谢谢了.

推荐答案

使用MVVM的方法是命令.您可以将它们视为声明性XAML和命令性VM之间的代理.

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

  1. 创建CreateNewProductCommand,实现ICommand.
  2. 为执行实际工作的命令创建一个处理程序,作为ICommand接口实现的一部分(通常称为On ***-OnCreateNewProductCommand)(您可能希望传递参数进行编辑,该接口也支持该参数).
  3. 将命令公开为VM的属性.
  4. 使用指向上一步创建的相关属性的标准绑定语法绑定该命令的菜单项命令(可能会找到它,仅搜索包含Command的属性)属性.

这不是唯一的方法.有一些基于交互/行为等的更高级的技术.其中一些可以使您绕过命令创建,并将UI元素事件直接绑定到VM的可执行成员.

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.

这篇关于MVVM中的MenuBarItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆