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); } } } } 这里大家注意的是,我的命令采用的不是一般的定义方式,而是采用字典集合的方式进行,好处上面讲了,请大家注意页面的绑定方式。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。