Mercurial > silverbladetech
diff SilverlightGlimpse/SilverlightValidation/ViewModels/UserViewModel.cs @ 78:dd6bcd2535b6
Working version
author | Steven Hollidge <stevenhollidge@hotmail.com> |
---|---|
date | Mon, 23 Apr 2012 22:43:53 +0100 |
parents | a0bcd783e612 |
children |
line wrap: on
line diff
--- a/SilverlightGlimpse/SilverlightValidation/ViewModels/UserViewModel.cs Mon Apr 23 22:06:05 2012 +0100 +++ b/SilverlightGlimpse/SilverlightValidation/ViewModels/UserViewModel.cs Mon Apr 23 22:43:53 2012 +0100 @@ -1,25 +1,18 @@ using System; -using System.ComponentModel; using System.Linq; -using System.Windows; -using System.Windows.Input; using FluentValidation; using SilverlightValidation.Interfaces; using SilverlightValidation.Validators; using SilverlightValidation.Models; -using SilverlightValidation.Commands; -using GalaSoft.MvvmLight.Messaging; -using SilverlightValidation.Messages; namespace SilverlightValidation.ViewModels { - public class UserViewModel : ViewModelBase, IUserModel, IEditableObject + public class UserViewModel : ViewModelBase, IUserModel { #region Fields private readonly UserModelValidator _validator; private UserModel _data; - private UserModel _backup; #endregion @@ -29,23 +22,6 @@ { _validator = validator; _data = model; - _backup = model.Clone(); - - OkCommand = new RelayCommand(OkCommandExecute); - CancelCommand = new RelayCommand(CancelCommandExecute); - } - - #endregion - - #region Methods - - private void SetProperties(IUserModel source) - { - _data.Username = source.Username; - _data.Password = source.Password; - _data.Email = source.Email; - _data.DateOfBirth = source.DateOfBirth; - _data.Description = source.Description; } #endregion @@ -62,7 +38,6 @@ { _data.Username = value; RaisePropertyChanged(UsernameProperty); - IsChanged = true; } ClearError(UsernameProperty); @@ -82,7 +57,6 @@ { _data.Password = value; RaisePropertyChanged(PasswordProperty); - IsChanged = true; } ClearError(PasswordProperty); @@ -102,7 +76,6 @@ { _data.Email = value; RaisePropertyChanged(EmailProperty); - IsChanged = true; } ClearError(EmailProperty); @@ -122,7 +95,6 @@ { _data.DateOfBirth = value; RaisePropertyChanged(DateOfBirthProperty); - IsChanged = true; } ClearError(DateOfBirthProperty); @@ -142,7 +114,6 @@ { _data.Description = value; RaisePropertyChanged(DescriptionProperty); - IsChanged = true; } ClearError(DescriptionProperty); @@ -153,71 +124,5 @@ } #endregion - - #region Commands - - public ICommand OkCommand { get; set; } - public ICommand CancelCommand { get; set; } - - private void OkCommandExecute(object obj) - { - RefreshToViewErrors(); - - if (IsChanged && !HasErrors) - { - // save here - Messenger.Default.Send<UserViewResponseMessage>( - new UserViewResponseMessage() { UserViewModel = this }); - } - } - - // in case user hasn't touched the form - private void RefreshToViewErrors() - { - Username = _data.Username; - Password = _data.Password; - Email = _data.Email; - DateOfBirth = _data.DateOfBirth; - } - - private void CancelCommandExecute(object obj) - { - Messenger.Default.Send<UserViewResponseMessage>( - new UserViewResponseMessage() { UserViewModel = null }); - } - - #endregion - - private void ResetFormData() - { - SetProperties(_backup); - ClearAllErrors(); - IsChanged = false; - } - - public bool IsChanged { get; private set; } - - #region IEditableObject for datagrid - - private bool inEdit; - public void BeginEdit() - { - if (inEdit) return; - inEdit = true; - } - - public void CancelEdit() - { - if (!inEdit) return; - inEdit = false; - ResetFormData(); - } - - public void EndEdit() - { - if (!inEdit) return; - } - - #endregion } }