1、项目结构图,如下图:
下面的windowsAero和WpfBase可以忽略掉。里面只是加了一个特效。
2、实体Model(Entity),如下图:
3、BLL和Dal我就不重复了。无非就是增删改查,对数据库的操作。
4、View视图,如下图:
这样整个项目就比较清晰了。
5、事件类,也就是绑定在view里面需要的事件
public class RelayCommand : ICommand
{
private bool _isenabled;
private Action _handler = null;
public RelayCommand(Action handler)
{
_handler = handler;
}
///
///
///
public bool IsEnabled
{
get { return _isenabled; }
set
{
_isenabled = value;
if (CanExecuteChanged != null)
{
this.CanExecuteChanged(this, EventArgs.Empty);
}
}
}
///
/// 是否启用
///
///
///
public bool CanExecute(object parameter)
{
//throw new NotImplementedException();
return IsEnabled;
}
public event EventHandler CanExecuteChanged;
///
/// 执行
///
///
public void Execute(object parameter)
{
//throw new NotImplementedException();
_handler();
}
}
定义好此类之后则可以开始做增删改查操作。
六、ViewModel
一、首先是baseViewModel
public class BaseViewModel
{
///
/// 服务
///
protected static ServiceReference1.Service1Client _service = null;
///
/// 用户姓名
///
protected static string UserName = null;
///
/// 用户ID
///
protected static int UserID = 0;
///
/// 级别ID
///
protected static int LevelID = 0;
///
/// 操作的窗体
///
protected Window windows = null;
///
///
///
public BaseViewModel()
{
if (_service == null)
{
_service = new ServiceReference1.Service1Client();//这个是我调用wcf的方法。
}
}
二、CompanyViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Model;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows;
public class CompanyInfoViewModel : BaseViewModel, INotifyPropertyChanged
{
private ObservableCollection
public ObservableCollection
{
get
{
return _companyInfoList;
}
set
{
if (_companyInfoList != value)
{
_companyInfoList = value;
OnPropertyChanged("CompanyInfoList");
}
}
}
public CompanyInfoViewModel()
{
//构造函数中初始化家庭成员列表
CompanyInfoModel model = new CompanyInfoModel();
List
_companyInfoList = new ObservableCollection
_addCompanyCommand = new RelayCommand(addCompanyCommand) { IsEnabled = true };
_searchCompanyCommand = new RelayCommand(searchCompanyCommand) { IsEnabled = true };
_delCompanyCommand = new RelayCommand(delCompanyCommand) { IsEnabled = true };
_updCompanyCommand = new RelayCommand(updCompanyCommand) { IsEnabled = true };
}
#region 绑定的事件
private ICommand _addCompanyCommand;
public ICommand AddCompanyCommand
{
get { return _addCompanyCommand; }
}
private ICommand _searchCompanyCommand;
public ICommand SearchCompanyCommand
{
get { return _searchCompanyCommand; }
}
private ICommand _delCompanyCommand;
public ICommand DelCompanyCommand
{
get { return _delCompanyCommand; }
}
private ICommand _updCompanyCommand;
public ICommand UpdCompanyCommand
{
get { return _updCompanyCommand; }
}
private CompanyInfoModel _companyModel = null;
public CompanyInfoModel CompanyModel
{
get
{
return _companyModel;
}
set
{
if (_companyModel != value)
{
_companyModel = value;
OnPropertyChanged("CompanyModel");
}
}
}
#endregion
#region 条件
private string _companyName = "";
public string CompanyName
{
get
{
return _companyName;
}
set
{
if (_companyName != value)
{
_companyName = value;
OnPropertyChanged("CompanyName");
}
}
}
private string _companyNo = "";
public string CompanyNo
{
get
{
return _companyNo;
}
set
{
if (_companyNo != value)
{
_companyName = value;
OnPropertyChanged("CompanyNo");
}
}
}
#endregion
#region 增删改查
///
/// 查询
///
public void searchCompanyCommand()
{
ObservableCollection
CompanyInfoModel model = new CompanyInfoModel();
if (!string.IsNullOrEmpty(CompanyName))
{
model.CompanyName = CompanyName;
}
if (!string.IsNullOrEmpty(CompanyNo))
{
model.CompanyNo = CompanyNo;
}
var companyList = _service.GetCompanyInfoByPage(0, model);
if (companyList != null)
{
list = new ObservableCollection
CompanyInfoList = list;
}
else
{
CompanyInfoList.Clear();
}
}
///
/// 删除
///
public void delCompanyCommand()
{
if (CompanyModel != null)
{
CompanyInfoList.Remove(CompanyModel);
MessageBox.Show("删除单位信息成功!");
}
else
{
MessageBox.Show("删除单位信息失败,请重试");
}
}
///
/// 修改数据
///
///
public void updEvenData(CompanyInfoModel model)
{
if (model != null)
{
if (windows != null)
{
windows.Close();
windows = null;
}
MessageBox.Show("更新成功");
}
}
///
/// 更新
///
public void updCompanyCommand()
{
SetCompanyInfoViewModel setCompanyView = new SetCompanyInfoViewModel();
AddCompanyInfo add = new AddCompanyInfo();
setCompanyView.addDataEvent += new addDataDelegate(updEvenData);
setCompanyView.Model = CompanyModel;
windows = add;
add.DataContext = setCompanyView;
add.ShowDialog();
}
///
///
///
///
public void addevenData(CompanyInfoModel model)
{
if (model != null)
{
// List
// list.Add(model);
// if (_service.AddCompanyInfoBeginTransaction(list.ToArray()))
// {
CompanyInfoList.Add(model);
if (windows != null)
{
windows.Close();
windows = null;
}
MessageBox.Show("添加成功");
//}
}
}
///
/// 添加数据
///
public void addCompanyCommand()
{
SetCompanyInfoViewModel setCompanyView = new SetCompanyInfoViewModel();
AddCompanyInfo add = new AddCompanyInfo();
setCompanyView.addDataEvent += new addDataDelegate(addevenData);
setCompanyView.Model = new CompanyInfoModel();
windows = add;
add.DataContext = setCompanyView;
add.ShowDialog();
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
在这里我要说明一下,在类里面我都去掉了命名空间, 这个东西需要你自己加上去。而不是直接复制下来拿来用就可以了
CompanyViewModel已经实现了,那么就是View了。
三、View如下图
绑定如下:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfBase;assembly=WpfBase"
Title="单位信息"
Height="500" Width="1000" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
ItemsSource="{Binding CompanyInfoList}"
CanUserDeleteRows="False" SelectedItem="{Binding CompanyModel,Mode=TwoWay}"
CanUserAddRows="False">
红色是因为我继承了这个类,如果你们没有继承,改成windows即可。
cs如下图:
这样列表页就已经完成。 在这里在啰嗦一下:WinFormsBase改成Window 即可。
在是增加和编辑 ViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Model;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows;
namespace OptWpfApp
{
///
///
///
///
public delegate void addDataDelegate(CompanyInfoModel model);
public class SetCompanyInfoViewModel : BaseViewModel, INotifyPropertyChanged
{
///
///
///
public event addDataDelegate addDataEvent;
public SetCompanyInfoViewModel()
{
_saveCompanyCommand = new RelayCommand(saveCompany) { IsEnabled = true };
}
///
/// 保存
///
public void saveCompany()
{
CompanyInfoModel model = new CompanyInfoModel()
{
CompanyNo = Model.CompanyNo,
CompanyName = Model.CompanyName,
RegisteredAddress = RegisteredAddress,
OfficeAddress = OfficeAddress,
CorporateRepresentative = CorporateRepresentative,
RegisteredCapital = RegisteredCapital,
ScopeBusiness = ScopeBusiness,
Code = Code,
CompanyCreateTime = CompanyCreateTime,
YingYeStartTime = YingYeStartTime,
YingYeEndTime = YingYeEndTime
};
if (addDataEvent != null)
{
addDataEvent(model);
}
}
#region 绑定的事件
private ICommand _saveCompanyCommand;
public ICommand saveCompanyCommand
{
get { return _saveCompanyCommand; }
}
#endregion
#region 绑定的实体
///
/// 公司简称代码,例如0000
///
private string _companyNo = "";
public string CompanyNo
{
get
{
return _companyNo;
}
set
{
if (_companyNo != value)
{
_companyNo = value;
OnPropertyChanged("CompanyNo");
}
}
}
///
/// 单位名称
///
private string _companyName = "";
public string CompanyName
{
get
{
return _companyName;
}
set
{
if (_companyName != value)
{
_companyName = value;
OnPropertyChanged("CompanyName");
}
}
}
///
/// 公司注册地址
///
private string _registeredAddress = "";
public string RegisteredAddress
{
get
{
return _registeredAddress;
}
set
{
if (_registeredAddress != value)
{
_registeredAddress = value;
OnPropertyChanged("RegisteredAddress");
}
}
}
///
/// 办公地址
///
private string _officeAddress = "";
public string OfficeAddress
{
get
{
return _officeAddress;
}
set
{
if (_officeAddress != value)
{
_officeAddress = value;
OnPropertyChanged("OfficeAddress");
}
}
}
///
/// 法人代表
///
private string _corporateRepresentative = "";
public string CorporateRepresentative
{
get
{
return _corporateRepresentative;
}
set
{
if (_corporateRepresentative != value)
{
_corporateRepresentative = value;
OnPropertyChanged("_corporateRepresentative");
}
}
}
///
/// 注册资金
///
private decimal _registeredCapital = 0;
public decimal RegisteredCapital
{
get
{
return _registeredCapital;
}
set
{
if (_registeredCapital != value)
{
_registeredCapital = value;
OnPropertyChanged("RegisteredCapital");
}
}
}
///
/// 经营范围
///
private string _scopeBusiness = "";
public string ScopeBusiness
{
get
{
return _scopeBusiness;
}
set
{
if (_scopeBusiness != value)
{
_scopeBusiness = value;
OnPropertyChanged("ScopeBusiness");
}
}
}
///
/// 组织机构代码
///
private string _code = "";
public string Code
{
get
{
return _code;
}
set
{
if (_code != value)
{
_code = value;
OnPropertyChanged("Code");
}
}
}
///
/// 成立日期
///
private DateTime _companyCreateTime = DateTime.MaxValue;
public DateTime CompanyCreateTime
{
get
{
return _companyCreateTime;
}
set
{
if (_companyCreateTime != value)
{
_companyCreateTime = value;
OnPropertyChanged("CompanyCreateTime");
}
}
}
///
/// 营业期限开始时间
///
///
private DateTime _yingYeStartTime = DateTime.MaxValue;
public DateTime YingYeStartTime
{
get
{
return _yingYeStartTime;
}
set
{
if (_yingYeStartTime != value)
{
_yingYeStartTime = value;
OnPropertyChanged("YingYeStartTime");
}
}
}
///
/// 营业期限结束时间
///
private DateTime _yingYeEndTime = DateTime.MaxValue;
public DateTime YingYeEndTime
{
get
{
return _yingYeEndTime;
}
set
{
if (_yingYeEndTime != value)
{
_yingYeEndTime = value;
OnPropertyChanged("YingYeEndTime");
}
}
}
public CompanyInfoModel _model;
public CompanyInfoModel Model
{
get
{
return _model;
}
set
{
if (_model != value)
{
_model = value;
OnPropertyChanged("_model");
}
}
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
View视图代码:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfBase;assembly=WpfBase"
Title="添加单位信息" Height="500" Width="900" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
另外里面引用的实体就是我上面定义的实体。
此次MVVM增删改查已经完成。
后台cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfBase;
using Model;
namespace OptWpfApp
{
///
/// AddCompanyInfo.xaml 的交互逻辑
///
public partial class AddCompanyInfo : WinFormsBase
{
public AddCompanyInfo()
{
InitializeComponent();
}
}
}
则是什么都没有。绑定的值全部都在ViewModel里面。