Mercurial > silverbladetech
annotate SilverlightValidation/SilverlightValidation/ViewModels/UserListViewModel.cs @ 60:fc62c971a117
upload to blog
author | Steven Hollidge <stevenhollidge@hotmail.com> |
---|---|
date | Sat, 21 Apr 2012 21:19:39 +0100 |
parents | 3591c26bd63e |
children |
rev | line source |
---|---|
58 | 1 using System.Collections.Generic; |
2 using System.Collections.ObjectModel; | |
3 using System.Windows.Input; | |
4 using SilverlightValidation.Commands; | |
5 using SilverlightValidation.Models; | |
6 using SilverlightValidation.Validators; | |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
7 using SilverlightValidation.Views; |
60 | 8 using GalaSoft.MvvmLight.Messaging; |
9 using SilverlightValidation.Messages; | |
58 | 10 |
11 namespace SilverlightValidation.ViewModels | |
12 { | |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
13 public class UserListViewModel |
58 | 14 { |
60 | 15 UserView window; |
16 | |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
17 public UserListViewModel(IList<UserModel> models, UserModelValidator validator) |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
18 { |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
19 Data = new ObservableCollection<UserViewModel>(); |
58 | 20 |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
21 foreach (var model in models) |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
22 Data.Add(new UserViewModel(model, validator)); |
58 | 23 |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
24 AddCommand = new RelayCommand(AddCommandExecute); |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
25 DeleteCommand = new RelayCommand(DeleteCommandExecute); |
60 | 26 |
27 Messenger.Default.Register<UserViewResponseMessage>(this, UserViewResponseMessageReceived); | |
28 } | |
29 | |
30 private void UserViewResponseMessageReceived(UserViewResponseMessage userViewResponseMessage) | |
31 { | |
32 if (userViewResponseMessage.UserViewModel != null) | |
33 Data.Add(userViewResponseMessage.UserViewModel); | |
34 window.Close(); | |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
35 } |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
36 |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
37 #region Properties |
58 | 38 |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
39 public ObservableCollection<UserViewModel> Data { get; set; } |
58 | 40 |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
41 public UserViewModel SelectedItem { get; set; } |
58 | 42 |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
43 #endregion |
58 | 44 |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
45 #region Commands |
58 | 46 |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
47 public ICommand AddCommand { get; set; } |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
48 public ICommand DeleteCommand { get; set; } |
58 | 49 |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
50 private void AddCommandExecute(object obj) |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
51 { |
60 | 52 window = new UserView(); |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
53 window.Show(); |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
54 } |
58 | 55 |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
56 private void DeleteCommandExecute(object obj) |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
57 { |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
58 if (SelectedItem!=null) |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
59 Data.Remove(SelectedItem); |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
60 } |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
61 |
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
58
diff
changeset
|
62 #endregion |
58 | 63 } |
64 } |