WPF +MVVM模式下的增删改查

本文介绍了使用WPF和MVVM模式进行数据库操作的步骤,包括项目结构、实体Model、BLL和Dal、View视图、事件类以及ViewModel的详细实现,特别是如何在ViewModel中定义并绑定增删改查命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
        }

        /// <summary>
        ///
        /// </summary>
        public bool IsEnabled
        {
            get { return _isenabled; }
            set
            {
                _isenabled = value;
                if (CanExecuteChanged != null)
                {
                    this.CanExecuteChanged(this, EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// 是否启用
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public bool CanExecute(object parameter)
        {
            //throw new NotImplementedException();
            return IsEnabled;
        }

        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="parameter"></param>
        public void Execute(object parameter)
        {
            //throw new NotImplementedException();
            _handler();
        }
    }

定义好此类之后则可以开始做增删改查操作。

六、ViewModel

   一、首先是baseViewModel

      public class BaseViewModel
    {
        /// <summary>
        /// 服务
        /// </summary>
        protected static ServiceReference1.Service1Client _service = null;
        /// <summary>
        /// 用户姓名
        /// </summary>
        protected static string UserName = null;
        /// <summary>
        /// 用户ID
        /// </summary>
        protected static int UserID = 0;
        /// <summary>
        /// 级别ID
        /// </summary>
        protected static int LevelID = 0;
        /// <summary>
        /// 操作的窗体
        /// </summary>
        protected Window windows = null;
        /// <summary>
        ///
        /// </summary>
        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<CompanyInfoModel> _companyInfoList;
        public ObservableCollection<CompanyInfoModel> CompanyInfoList
        {
            get
            {
                return _companyInfoList;
            }
            set
            {
                if (_companyInfoList != value)
                {
                    _companyInfoList = value;
                    OnPropertyChanged("CompanyInfoList");
                }
            }
        }

        public CompanyInfoViewModel()
        {
            //构造函数中初始化家庭成员列表
            CompanyInfoModel model = new CompanyInfoModel();
            List<CompanyInfoModel> list = _service.GetCompanyInfoByPage(0, model).ToList();
            _companyInfoList = new ObservableCollection<CompanyInfoModel>(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");
                }
           

评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值