3)視圖模型層DynamicDataViewModel .cs
~~~
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MAppStructure.Datasource;
using System.ComponentModel;
using System.Collections.Generic;
using MEntities;
namespace MAppStructure.ViewModel
{
?? /// <summary>
?? /// 視圖模型層,注意必須實現INotifyPropertyChanged接口,這樣VM層才能綁定。
?? /// ViewModel可以做一個基類,這個基類主要是實現INotifyPropertyChanged接口
?? /// 和你需要的一些公共方法,這里為了簡單些,就沒有做基類了.
?? /// </summary>
?? public class DynamicDataViewModel : INotifyPropertyChanged
?? {
?????? /// <summary>
?????? /// 視圖模型層主要采用的模型層服務實例,注意
?????? /// 可以調用多個模型層得服務實例,這里是演示,所以只做了一個。
?????? /// </summary>
?????? private DynamicDataSource theS;
?????? public DynamicDataViewModel()
?????? {
?????????? theS = new DynamicDataSource();
?????????? //初始化并注冊命令.
?????????? Commands = new Dictionary<string, MyCommand>();
?????????? Commands.Add("Button1Command", new MyCommand(OnButtonCommand));
?????? }
?????? private void OnButtonCommand(object parameter)
?????? {
?????????? LoadData();
?????????? MessageBox.Show("ok");
?????? }
??????
?????? /// <summary>
?????? ///命令字典集合,這樣做的好處一是可以減少定義命令的硬代碼,同時提供了一種動
?????? ///態命令的可能性,并有利于擴展.
?????? /// </summary>
?????? public Dictionary<string, MyCommand> Commands
?????? {
?????????? get;
?????????? private set;
?????? }
?????? private List<DynamicDataRow> _DataSource;
?????? public List<DynamicDataRow> DataSource
?????? {
?????????? get
?????????? {
?????????????? return _DataSource;
?????????? }
?????????? private set
?????????? {
?????????????? _DataSource = value;
?????????????? RaisePropertyChanged("DataSource");
?????????????
?????????? }
?????? }
?????? private DynamicDataTable _DataTable;
?????? /// <summary>
?????? /// 獲取來的動態數據表.
?????? /// </summary>
?????? public DynamicDataTable DataTable
?????? {
?????????? get
?????????? {
?????????????? return _DataTable;
?????????? }
?????????? private set
?????????? {
?????????????? _DataTable = value;
?????????????? RaisePropertyChanged("DataTable");
?????????? }
?????? }
?????? /// <summary>
?????? /// 數據加載.
?????? /// </summary>
?????? private void LoadData()
?????? {
?????????? theS.GetDynamicDataTable("select * from EmployeeInfo ", op =>
?????????????? {
?????????????????? if (op.HasError == false)
?????????????????? {
?????????????????????? DataSource = op.Value.Rows;
?????????????????????? DataTable = op.Value;
?????????????????? }
?????????????????? else
?????????????????? {
?????????????????????? MessageBox.Show(op.ErrorMsg);
?????????????????? }
?????????????? }, null);
?????? }
?????? public event PropertyChangedEventHandler PropertyChanged;
?????? protected void RaisePropertyChanged(string propertyName)
?????? {
?????????? var handler = PropertyChanged;
?????????? if (handler != null)
?????????? {
?????????????? handler(this, new PropertyChangedEventArgs(propertyName));
?????????? }
?????? }
?? }
?? /// <summary>
?? /// 自己的命令類,主要為了命令的綁定.這里是典型的命令模式,命令的接收者是本VM.不過這里的
?? /// 命令接收者并沒有作為命令的成員,而是采用委托方式,在這種情況下更為便利。
?? /// </summary>
?? public class MyCommand : ICommand
?? {
?????? public bool CanExecute(object parameter)
?????? {
?????????? return true;
?????? }
?????? private Action<object> _action;
?????? public MyCommand(Action<object> Action)
?????? {
?????????? this._action = Action;
?????? }
?????? public event EventHandler CanExecuteChanged;
?????? public void Execute(object parameter)
?????? {
?????????? if (_action != null)
?????????? {
?????????????? _action(parameter);
?????????? }
?????? }
?? }
}
~~~
這里大家注意的是,我的命令采用的不是一般的定義方式,而是采用字典集合的方式進行,好處上面講了,請大家注意頁面的綁定方式。