public UserManagerViewmodel(IAuthenticationService authenticationService)
 {
     _authenticationService = authenticationService;
     Groups = new ObservableCollection <GroupViewmodel>(authenticationService.Groups.Select(g =>
     {
         var newVm = new GroupViewmodel(g);
         newVm.Load();
         return(newVm);
     }));
     Users = new ObservableCollection <UserViewmodel>(authenticationService.Users.Select(u =>
     {
         var newVm = new UserViewmodel(u, this);
         newVm.Load();
         return(newVm);
     }));
     CommandAddUser = new UICommand {
         ExecuteDelegate = AddUser
     };
     CommandDeleteUser = new UICommand {
         ExecuteDelegate = DeleteUser, CanExecuteDelegate = CanDeleteUser
     };
     CommandAddGroup = new UICommand {
         ExecuteDelegate = AddGroup
     };
     CommandDeleteGroup = new UICommand {
         ExecuteDelegate = DeleteGroup, CanExecuteDelegate = CanDeleteGroup
     };
     authenticationService.UsersOperation  += AuthenticationService_UsersOperation;
     authenticationService.GroupsOperation += AuthenticationService_GroupsOperation;
 }
        private void AddGroup(object obj)
        {
            var newVm = new GroupViewmodel(_authenticationService.CreateGroup());

            Groups.Add(newVm);
            SelectedGroup = newVm;
        }
 private void AuthenticationService_GroupsOperation(object sender, CollectionOperationEventArgs <IGroup> e)
 {
     Application.Current.Dispatcher.BeginInvoke((Action) delegate
     {
         var vm = Groups.FirstOrDefault(u => u.Model == e.Item);
         if (e.Operation == CollectionOperation.Add)
         {
             if (vm == null)
             {
                 vm = new GroupViewmodel(e.Item);
                 vm.Load();
                 Groups.Add(vm);
             }
         }
         else
         {
             if (vm != null)
             {
                 Groups.Remove(vm);
                 vm.Dispose();
             }
         }
     });
 }