comparison SilverlightGlimpse/SilverlightValidation/ViewModels/UserListViewModel.cs @ 69:a0bcd783e612

Latest work
author Steven Hollidge <stevenhollidge@hotmail.com>
date Mon, 23 Apr 2012 11:06:10 +0100
parents
children 96e6fbd70f49
comparison
equal deleted inserted replaced
68:81337ebf885a 69:a0bcd783e612
1 using System.Collections.ObjectModel;
2 using System.Windows.Input;
3 using SilverlightValidation.Commands;
4 using SilverlightValidation.Models;
5 using SilverlightValidation.Validators;
6 using SilverlightValidation.Views;
7 using GalaSoft.MvvmLight.Messaging;
8 using SilverlightValidation.Messages;
9
10 namespace SilverlightValidation.ViewModels
11 {
12 public class UserListViewModel
13 {
14 UserView window;
15
16 public UserListViewModel(IList<UserModel> models, UserModelValidator validator)
17 {
18 Data = new ObservableCollection<UserViewModel>();
19
20 foreach (var model in models)
21 Data.Add(new UserViewModel(model, validator));
22
23 AddCommand = new RelayCommand(AddCommandExecute);
24 DeleteCommand = new RelayCommand(DeleteCommandExecute);
25
26 Messenger.Default.Register<UserViewResponseMessage>(this, UserViewResponseMessageReceived);
27 }
28
29 private void UserViewResponseMessageReceived(UserViewResponseMessage userViewResponseMessage)
30 {
31 if (userViewResponseMessage.UserViewModel != null)
32 Data.Add(userViewResponseMessage.UserViewModel);
33 window.Close();
34 }
35
36 #region Properties
37
38 public ObservableCollection<UserViewModel> Data { get; set; }
39
40 public UserViewModel SelectedItem { get; set; }
41
42 #endregion
43
44 #region Commands
45
46 public ICommand AddCommand { get; set; }
47 public ICommand DeleteCommand { get; set; }
48
49 private void AddCommandExecute(object obj)
50 {
51 window = new UserView();
52 window.Show();
53 }
54
55 private void DeleteCommandExecute(object obj)
56 {
57 if (SelectedItem!=null)
58 Data.Remove(SelectedItem);
59 }
60
61 #endregion
62 }
63 }