comparison delete me/SilverlightValidation/ViewModels/UserViewModel.cs @ 59:3591c26bd63e

MVVMLight added
author Steven Hollidge <stevenhollidge@hotmail.com>
date Sat, 21 Apr 2012 19:20:28 +0100
parents
children
comparison
equal deleted inserted replaced
58:241e2f22ed3c 59:3591c26bd63e
1 using System;
2 using System.ComponentModel;
3 using System.Linq;
4 using System.Windows;
5 using System.Windows.Input;
6 using FluentValidation;
7 using SilverlightValidation.Interfaces;
8 using SilverlightValidation.Validators;
9 using SilverlightValidation.Models;
10 using SilverlightValidation.Commands;
11
12 namespace SilverlightValidation.ViewModels
13 {
14 public class UserViewModel : ViewModelBase, IUserModel, IChangeTracking, IEditableObject
15 {
16 #region Fields
17
18 private readonly UserModelValidator _validator;
19 private UserModel _data;
20 private UserModel _backup;
21
22 #endregion
23
24 #region Constructor
25
26 public UserViewModel(UserModel model, UserModelValidator validator)
27 {
28 _validator = validator;
29 _data = model;
30 _backup = model.Clone();
31
32 OkCommand = new RelayCommand(OkCommandExecute);
33 CancelCommand = new RelayCommand(CancelCommandExecute);
34 }
35
36 #endregion
37
38 #region Methods
39
40 private void SetProperties(IUserModel source)
41 {
42 Username = source.Username;
43 Password = source.Password;
44 Email = source.Email;
45 DateOfBirth = source.DateOfBirth;
46 Description = source.Description;
47 }
48
49 #endregion
50
51 #region Properties
52
53 private const string UsernameProperty = "Username";
54 public string Username
55 {
56 get { return _data.Username; }
57 set
58 {
59 if (_data.Username != value)
60 {
61 _data.Username = value;
62 RaisePropertyChanged(UsernameProperty);
63 IsChanged = true;
64 }
65
66 ClearError(UsernameProperty);
67 var validationResult = _validator.Validate(this, UsernameProperty);
68 if (!validationResult.IsValid)
69 validationResult.Errors.ToList().ForEach(x => SetError(UsernameProperty, x.ErrorMessage));
70 }
71 }
72
73 private const string PasswordProperty = "Password";
74 public string Password
75 {
76 get { return _data.Password; }
77 set
78 {
79 if (_data.Password != value)
80 {
81 _data.Password = value;
82 RaisePropertyChanged(PasswordProperty);
83 IsChanged = true;
84 }
85
86 ClearError(PasswordProperty);
87 var validationResult = _validator.Validate(this, PasswordProperty);
88 if (!validationResult.IsValid)
89 validationResult.Errors.ToList().ForEach(x => SetError(PasswordProperty, x.ErrorMessage));
90 }
91 }
92
93 private const string EmailProperty = "Email";
94 public string Email
95 {
96 get { return _data.Email; }
97 set
98 {
99 if (_data.Email != value)
100 {
101 _data.Email = value;
102 RaisePropertyChanged(EmailProperty);
103 IsChanged = true;
104 }
105
106 ClearError(EmailProperty);
107 var validationResult = _validator.Validate(this, EmailProperty);
108 if (!validationResult.IsValid)
109 validationResult.Errors.ToList().ForEach(x => SetError(EmailProperty, x.ErrorMessage));
110 }
111 }
112
113 private const string DateOfBirthProperty = "DateOfBirth";
114 public DateTime? DateOfBirth
115 {
116 get { return _data.DateOfBirth; }
117 set
118 {
119 if (_data.DateOfBirth != value)
120 {
121 _data.DateOfBirth = value;
122 RaisePropertyChanged(DateOfBirthProperty);
123 IsChanged = true;
124 }
125
126 ClearError(DateOfBirthProperty);
127 var validationResult = _validator.Validate(this, DateOfBirthProperty);
128 if (!validationResult.IsValid)
129 validationResult.Errors.ToList().ForEach(x => SetError(DateOfBirthProperty, x.ErrorMessage));
130 }
131 }
132
133 private const string DescriptionProperty = "Description";
134 public string Description
135 {
136 get { return _data.Description; }
137 set
138 {
139 if (_data.Description != value)
140 {
141 _data.Description = value;
142 RaisePropertyChanged(DescriptionProperty);
143 IsChanged = true;
144 }
145
146 ClearError(DescriptionProperty);
147 var validationResult = _validator.Validate(this, DescriptionProperty);
148 if (!validationResult.IsValid)
149 validationResult.Errors.ToList().ForEach(x => SetError(DescriptionProperty, x.ErrorMessage));
150 }
151 }
152
153 #endregion
154
155 #region Commands
156
157 public ICommand OkCommand { get; set; }
158 public ICommand CancelCommand { get; set; }
159
160 private void OkCommandExecute(object obj)
161 {
162 SetProperties(_data);
163
164 if (IsChanged && !HasErrors)
165 {
166 AcceptChanges();
167 }
168 }
169
170 private void CancelCommandExecute(object obj)
171 {
172 CancelChanges();
173 }
174
175 #endregion
176
177 #region IChangeTrack plus Cancel
178
179 public void AcceptChanges()
180 {
181 MessageBox.Show("Saving...");
182 SetProperties(_backup);
183
184 ClearAllErrors();
185 IsChanged = false;
186 }
187
188 public void CancelChanges()
189 {
190 if (!IsChanged) return;
191 SetProperties(_backup);
192 ClearAllErrors();
193 IsChanged = false;
194 }
195
196 public bool IsChanged { get; private set; }
197
198 #endregion
199
200 #region
201
202 private bool inEdit;
203 public void BeginEdit()
204 {
205 if (inEdit) return;
206 inEdit = true;
207 SetProperties(_backup);
208 }
209
210 public void CancelEdit()
211 {
212 if (!inEdit) return;
213 inEdit = false;
214 CancelChanges();
215 }
216
217 public void EndEdit()
218 {
219 if (!inEdit) return;
220 inEdit = false;
221 SetProperties(_backup);
222 }
223
224 #endregion
225
226 }
227 }