public HomeViewModel() { try { if (Users == null) Users = new ObservableCollection<User>(); if (UserInfo == null) UserInfo = new User(); MCountry = new ObservableCollection<string>( from c in context.Users group c by c.Country into g select g.Key); context = new Amol_LearningEntities(); CreateCommand = new RelayCommand<User>(CreateUser, CreateCanExecute); DeleteCommand = new RelayCommand<User>(DeleteUser); EditCommand = new RelayCommand<User>(param => EditUser(param)); NewCommand = new RelayCommand<string>(param => NewUser(param)); ReceiveUserInfo(); RefreshData(); } catch (Exception ex) { throw new Exception(ex.Message); } }
private void NewUser(string obj) { User userObj = new User(); userObj.Id = Convert.ToInt32(context.Users.Select(x => x.Id).Max()) + 1; var msg = new GoToMessagePage() { UserObj = userObj, PageName = "NewView", ButtonContentMsg = obj }; Messenger.Default.Send<GoToMessagePage>(msg); }
private void EditUser(User userObj) { if (userObj != null) { var msg = new GoToMessagePage() { UserObj = userObj, PageName = "NewView", ButtonContentMsg = "Update" }; Messenger.Default.Send<GoToMessagePage>(msg); } }
private void DeleteUser(User param) { MessageBoxResult messageBoxResult = MessageBox.Show("Are you sure?", "Delete Confirmation", MessageBoxButton.YesNo); if (messageBoxResult == MessageBoxResult.Yes) { context.Users.Remove(param); context.SaveChanges(); RefreshData(); } }
private void CreateUser(User obj) { try { obj.LastName = "Test"; obj.Password = "******"; context.Users.Add(obj); context.SaveChanges(); RefreshData(); MessageBox.Show("New User Created Sucessfully"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
bool CreateCanExecute(User p_objUser) { Regex regxEmail = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); bool isUserName = String.IsNullOrEmpty(p_objUser.UserName) ? false : true; bool isCountry = String.IsNullOrEmpty(SelectedCountry) ? false : true; bool isCity = String.IsNullOrEmpty(p_objUser.City) ? false : true; bool isAddress = String.IsNullOrEmpty(p_objUser.Address) ? false : true; bool isEmail = String.IsNullOrEmpty(p_objUser.email) ? false : (regxEmail.Match(p_objUser.email) == Match.Empty) ? false : true; if (isUserName && isCountry && isCity && isEmail && isAddress) { return true; } return false; }