Mercurial > silverbladetech
view delete me/SilverlightValidation/ViewModels/UserViewModel.cs @ 66:b41cd0075c35
Third count added
author | Steven Hollidge <stevenhollidge@hotmail.com> |
---|---|
date | Sun, 22 Apr 2012 16:00:41 +0100 |
parents | 3591c26bd63e |
children |
line wrap: on
line source
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; namespace SilverlightValidation.ViewModels { public class UserViewModel : ViewModelBase, IUserModel, IChangeTracking, IEditableObject { #region Fields private readonly UserModelValidator _validator; private UserModel _data; private UserModel _backup; #endregion #region Constructor public UserViewModel(UserModel model, UserModelValidator validator) { _validator = validator; _data = model; _backup = model.Clone(); OkCommand = new RelayCommand(OkCommandExecute); CancelCommand = new RelayCommand(CancelCommandExecute); } #endregion #region Methods private void SetProperties(IUserModel source) { Username = source.Username; Password = source.Password; Email = source.Email; DateOfBirth = source.DateOfBirth; Description = source.Description; } #endregion #region Properties private const string UsernameProperty = "Username"; public string Username { get { return _data.Username; } set { if (_data.Username != value) { _data.Username = value; RaisePropertyChanged(UsernameProperty); IsChanged = true; } ClearError(UsernameProperty); var validationResult = _validator.Validate(this, UsernameProperty); if (!validationResult.IsValid) validationResult.Errors.ToList().ForEach(x => SetError(UsernameProperty, x.ErrorMessage)); } } private const string PasswordProperty = "Password"; public string Password { get { return _data.Password; } set { if (_data.Password != value) { _data.Password = value; RaisePropertyChanged(PasswordProperty); IsChanged = true; } ClearError(PasswordProperty); var validationResult = _validator.Validate(this, PasswordProperty); if (!validationResult.IsValid) validationResult.Errors.ToList().ForEach(x => SetError(PasswordProperty, x.ErrorMessage)); } } private const string EmailProperty = "Email"; public string Email { get { return _data.Email; } set { if (_data.Email != value) { _data.Email = value; RaisePropertyChanged(EmailProperty); IsChanged = true; } ClearError(EmailProperty); var validationResult = _validator.Validate(this, EmailProperty); if (!validationResult.IsValid) validationResult.Errors.ToList().ForEach(x => SetError(EmailProperty, x.ErrorMessage)); } } private const string DateOfBirthProperty = "DateOfBirth"; public DateTime? DateOfBirth { get { return _data.DateOfBirth; } set { if (_data.DateOfBirth != value) { _data.DateOfBirth = value; RaisePropertyChanged(DateOfBirthProperty); IsChanged = true; } ClearError(DateOfBirthProperty); var validationResult = _validator.Validate(this, DateOfBirthProperty); if (!validationResult.IsValid) validationResult.Errors.ToList().ForEach(x => SetError(DateOfBirthProperty, x.ErrorMessage)); } } private const string DescriptionProperty = "Description"; public string Description { get { return _data.Description; } set { if (_data.Description != value) { _data.Description = value; RaisePropertyChanged(DescriptionProperty); IsChanged = true; } ClearError(DescriptionProperty); var validationResult = _validator.Validate(this, DescriptionProperty); if (!validationResult.IsValid) validationResult.Errors.ToList().ForEach(x => SetError(DescriptionProperty, x.ErrorMessage)); } } #endregion #region Commands public ICommand OkCommand { get; set; } public ICommand CancelCommand { get; set; } private void OkCommandExecute(object obj) { SetProperties(_data); if (IsChanged && !HasErrors) { AcceptChanges(); } } private void CancelCommandExecute(object obj) { CancelChanges(); } #endregion #region IChangeTrack plus Cancel public void AcceptChanges() { MessageBox.Show("Saving..."); SetProperties(_backup); ClearAllErrors(); IsChanged = false; } public void CancelChanges() { if (!IsChanged) return; SetProperties(_backup); ClearAllErrors(); IsChanged = false; } public bool IsChanged { get; private set; } #endregion #region private bool inEdit; public void BeginEdit() { if (inEdit) return; inEdit = true; SetProperties(_backup); } public void CancelEdit() { if (!inEdit) return; inEdit = false; CancelChanges(); } public void EndEdit() { if (!inEdit) return; inEdit = false; SetProperties(_backup); } #endregion } }