背景:
需求:实现从数据库读取级联表指定字段数据,并展示到前台界面。
VM层做业务逻辑层,每页最多获取2条数据。
View层只有数据表格,上一页与下一页按钮,且上一页与下一页在特定条件下不可用。
(转载请注明来源:cnblogs coder-fang)
解决方案结构如下:
-
-
- 项目结构:
- WPFTest:主要是界面显示数据(V层),ViewModel是wpftest的VM层,unittest做vm及数据的测试项目。
- 示例用的数据结构如图:
- 项目结构:
-
- 创建类库VM项目,为了简化,这里将M层与VM层放到了同一项目中,首先在VM中使用EF框架生成相关数据实体与context(EF自行查阅,这里不多介绍),即M层,项目目录如下:
- 为了使V层(展示层)与核心业务控制解耦,需要在VM层实现对业务的控制,建通用Command类,代码如下:
View Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; namespace ViewModels { public class Command :ICommand { private Action methodToExecute = null; private Func<bool> methodCanExecute = null; public Command(Action methodToExecute, Func<bool> methodCanExecute) { this.methodToExecute = methodToExecute; this.methodCanExecute = methodCanExecute; } public void Execute(object parameter) { this.methodToExecute(); } public bool CanExecute(object parameter) { if (this.methodCanExecute == null) { return true; } else { return this.methodCanExecute(); } } public event EventHandler CanExecuteChanged; public void RaseCanExecuteChangedEvent() { if (this.CanExecuteChanged != null) { this.CanExecuteChanged(this, EventArgs.Empty); } } } }