这相对于上一版本的升级版
如果不理解看请看第一版:WPF+SqlSugar+MVVM实现增删改查 - 六子12138 - 博客园 (cnblogs.com)
ViewModels代码
1 using Entitys; 2 using SqlSugar; 3 using System; 4 using System.Collections.Generic; 5 using System.Collections.ObjectModel; 6 using System.ComponentModel; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Windows.Controls; 11 using WpfMVVM.Model; 12 using WpfMVVM.Views; 13 14 namespace WpfMVVM.ViewModel 15 { 16 public class UserViewModel : INotifyPropertyChanged 17 { 18 public event PropertyChangedEventHandler PropertyChanged; 19 protected void OnPropertyChanged(string PropertyName) 20 { 21 if (PropertyChanged != null) 22 { 23 PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); 24 } 25 } 26 27 SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() 28 { 29 //数据库配置 30 ConnectionString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=123456", 31 32 DbType = DbType.SqlServer,//设置数据库类型 33 34 IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放 35 36 InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息 37 38 }); 39 40 41 private ObservableCollection<Users> userList; 42 /// <summary> 43 /// 用户集合 44 /// </summary> 45 public ObservableCollection<Users> UserList 46 { 47 get 48 { 49 if (userList == null) 50 { 51 userList = new ObservableCollection<Users>(); 52 //设置数据源 //也可以直接写查询 53 userList = new ObservableCollection<Users>(db.Queryable<Users>().ToList()); 54 } 55 return userList; 56 } 57 set { 58 if (userList == null) 59 { 60 userList = new ObservableCollection<Users>(); 61 } 62 userList = value; 63 //当UserList实体的数据发生改变时实现属性更改通知 64 OnPropertyChanged("UserList"); 65 } 66 } 67 68 private Users selectWhere; 69 70 public Users SelectWhere 71 { 72 get 73 { 74 if (selectWhere == null) 75 { 76 selectWhere = new Users(); 77 selectWhere.U_Name = ""; 78 selectWhere.U_Sex = "全部"; 79 //多条件查询的初始化 80 } 81 return selectWhere; 82 } 83 set 84 { 85 if (selectWhere == null) 86 { 87 selectWhere = new Users(); 88 } 89 selectWhere = value; 90 //当SelectWhere实体的数据发生改变时实现属性更改通知 91 OnPropertyChanged("SelectWhere"); 92 } 93 } 94 95 96 public Users selectUser; 97 /// <summary> 98 /// 被选中的用户 99 /// </summary> 100 public Users SelectUser 101 { 102 get 103 { 104 if (selectUser == null) 105 { 106 selectUser = new Users(); 107 108 } 109 return selectUser; 110 } 111 set 112 { 113 if (selectUser == null) 114 { 115 selectUser = new Users(); 116 } 117 selectUser = value; 118 //当SelectUser实体的数据发生改变时实现属性更改通知 119 OnPropertyChanged("SelectUser"); 120 } 121 } 122 123 public UserViewModel() 124 { 125 //注册命令 126 RegisterCommands(); 127 } 128 129 130 131 /// <summary> 132 /// 注册命令 133 /// </summary> 134 private void RegisterCommands() 135 { 136 SelectionWhereCommand = new DelegateCommands(); 137 SelectionWhereCommand.ExecuteCommand = new Action<object>(SelectionWhere); 138 AddCommand = new DelegateCommands(); 139 AddCommand.ExecuteCommand = new Action<object>(Add); 140 UpdateCommand = new DelegateCommands(); 141 UpdateCommand.ExecuteCommand = new Action<object>(Update); 142 IsokCommand = new DelegateCommands(); 143 IsokCommand.ExecuteCommand = new Action<object>(Isok); 144 DeleteCommand = new DelegateCommands(); 145 DeleteCommand.ExecuteCommand = new Action<object>(Delete); 146 } 147 AddOrUpdate au = new AddOrUpdate(); 148 /// <summary> 149 /// 通过构造方法将实体数据,和窗体实体传入新的UserViewModel中 150 /// </summary> 151 /// <param name="_selectUser"></param> 152 /// <param name="_au"></param> 153 public UserViewModel(Users _selectUser ,AddOrUpdate _au) 154 { 155 SelectUser = _selectUser; 156 au = _au; 157 //注册命令 158 RegisterCommands(); 159 } 160 /// <summary> 161 /// 显示提示信息 162 /// </summary> 163 /// <param name="txt"></param> 164 public void MsgShow(string txt) 165 { 166 System.Windows.MessageBox.Show(txt); 167 //删除后清空选择用户 168 SelectUser = null; 169 } 170 #region 注册命令 171 /// <summary> 172 /// 新增用户 173 /// </summary> 174 175 /// <summary> 176 /// 多条件查询 177 /// </summary> 178 public DelegateCommands SelectionWhereCommand { get; set; } 179 /// <summary> 180 /// 添加 181 /// </summary> 182 public DelegateCommands AddCommand { get; set; } 183 /// <summary> 184 /// 修改 185 /// </summary> 186 public DelegateCommands UpdateCommand { get; set; } 187 /// <summary> 188 /// 保存 添加or修改 189 /// </summary> 190 public DelegateCommands IsokCommand { get; set; } 191 /// <summary> 192 /// 删除 193 /// </summary> 194 public DelegateCommands DeleteCommand { get; set; } 195 196 /// <summary> 197 /// 刷新数据 198 /// </summary> 199 /// <param name="parameter"></param> 200 public void SelectionWhere(object parameter) 201 { 202 UserList = new ObservableCollection<Users>(db.Queryable<Users>().Where(u => ((selectWhere.U_Name == "") || (u.U_Name.Contains(selectWhere.U_Name))) && ((selectWhere.U_Sex == "全部") || (u.U_Sex == selectWhere.U_Sex))).ToList()); 203 } 204 /// <summary> 205 /// 新增弹出窗体 206 /// </summary> 207 /// <param name="parameter"></param> 208 public void Add(object parameter) 209 { 210 AddOrUpdate add = new AddOrUpdate(null); 211 add.ShowDialog();//因为ShowDialog后,新的窗体未关闭,下面的代码不会执行,当关闭后会执行下面的刷新数据操作 212 SelectionWhere(null); 213 } 214 /// <summary> 215 /// 修改弹出窗体 216 /// </summary> 217 /// <param name="parameter"></param> 218 public void Update(object parameter) 219 { 220 //parameter 因为在XML前端进行绑定的时候传入参数ID 221 //下面进行类型转换就可以使用了 222 Users u = db.Queryable<Users>().Where(us => us.U_ID == Convert.ToInt32(parameter)).First(); 223 AddOrUpdate add = new AddOrUpdate(u); 224 add.ShowDialog();//因为ShowDialog后,新的窗体未关闭,下面的代码不会执行,当关闭后会执行下面的刷新数据操作 225 SelectionWhere(null); 226 } 227 /// <summary> 228 /// 保存 新增or修改执行操作 229 /// </summary> 230 /// <param name="parameter"></param> 231 public void Isok(object parameter) 232 { //因为没有写部门的控件就将就设置一下 233 SelectUser.D_ID = 1; 234 //判断这个实体有没有ID,如果没有执行新增,反之执行修改 235 if(SelectUser.U_ID == null) 236 { 237 var t3 = db.Insertable(SelectUser).ExecuteReturnEntity(); 238 MsgShow("用户编号:" + t3.U_ID + "\n新增成功!"); 239 //之前通过构造函数将窗体的实例传过来了,执行关闭窗体 240 au.Close(); 241 } 242 else 243 { 244 var t1 = db.Updateable(SelectUser).ExecuteCommand(); 245 MsgShow("用户编号:" + SelectUser.U_ID + "\n修改成功!"); 246 //之前通过构造函数将窗体的实例传过来了,执行关闭窗体 247 au.Close(); 248 } 249 } 250 /// <summary> 251 /// 删除 252 /// </summary> 253 /// <param name="parameter"></param> 254 public void Delete(object parameter) 255 { 256 //parameter 因为在XML前端进行绑定的时候传入参数ID 257 //下面进行类型转换就可以使用了 258 var t0 = db.Deleteable<Users>().Where(u => u.U_ID == Convert.ToInt32(parameter)).ExecuteCommand(); 259 MsgShow("用户编号:" + Convert.ToInt32(parameter) + "\n删除成功!"); 260 SelectionWhere(null); 261 } 262 263 264 265 #endregion 266 } 267 }
ViewModels里面CheckConverter.cs这是一个转换器,绑定单选按钮会用到
1 using System; 2 using System.Collections.Generic; 3 using System.Globalization; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Windows.Data; 8 9 namespace WpfMVVM.ViewModels 10 { 11 public class CheckConverter : IValueConverter 12 { 13 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 14 { 15 if (value == null || parameter == null) 16 { 17 return false; 18 } 19 string checkvalue = value.ToString(); 20 string targetvalue = parameter.ToString(); 21 bool r = checkvalue.Equals(targetvalue); 22 return r; 23 } 24 25 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 26 { 27 if (value == null || parameter == null) 28 { 29 return null; 30 } 31 32 if ((bool)value) 33 { 34 return parameter.ToString(); 35 } 36 return null; 37 } 38 } 39 40 }
标签:MVVM,改查,System,using,new,WPF,null,parameter,public From: https://www.cnblogs.com/liuzi12138/p/17928010.html