WPF +MVVM模式下的增删改查

1、项目结构图,如下图:

WPF +MVVM模式下的增删改查_第1张图片

下面的windowsAero和WpfBase可以忽略掉。里面只是加了一个特效。

2、实体Model(Entity),如下图:

WPF +MVVM模式下的增删改查_第2张图片

3、BLL和Dal我就不重复了。无非就是增删改查,对数据库的操作。

4、View视图,如下图:

WPF +MVVM模式下的增删改查_第3张图片

这样整个项目就比较清晰了。

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 _companyInfoList;
        public ObservableCollection CompanyInfoList
        {
            get
            {
                return _companyInfoList;
            }
            set
            {
                if (_companyInfoList != value)
                {
                    _companyInfoList = value;
                    OnPropertyChanged("CompanyInfoList");
                }
            }
        }

        public CompanyInfoViewModel()
        {
            //构造函数中初始化家庭成员列表
            CompanyInfoModel model = new CompanyInfoModel();
            List list = _service.GetCompanyInfoByPage(0, model).ToList();
            _companyInfoList = new ObservableCollection(list);

            _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 list = null;
            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(companyList.ToList());
                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 = new 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如下图

WPF +MVVM模式下的增删改查_第4张图片

绑定如下:

x:Class="OptWpfApp.CompanyInfo"
                  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">
   
       
           
           
       

       

你可能感兴趣的:(MVVM,WPF,c#)