-
-
Save Suchiman/41fc2331be66d506e485e7fa3e38184c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.ComponentModel; | |
using System.Diagnostics; | |
using System.Runtime.CompilerServices; | |
using ProjectPatronBackoffice.Models; | |
using ProjectPatronBackoffice.Repositories; | |
using GalaSoft.MvvmLight.Command; | |
namespace ProjectPatronBackoffice.ViewModels | |
{ | |
public class DepartmentViewModel : INotifyPropertyChanged | |
{ | |
private ObservableCollection<Department> _departmentList; | |
public event PropertyChangedEventHandler PropertyChanged; | |
public ObservableCollection<Department> DepartmentList | |
{ | |
get { return _departmentList; } | |
set { SetField(ref _departmentList, value); } | |
} | |
// Constructor | |
public DepartmentViewModel() | |
{ | |
Department d = new Department {DepartmentId = 0, DepartmentName = "Test", DepartmentNumber = 4}; | |
this.FetchDepartmentCommand = new RelayCommand(FetchDepartments); | |
} | |
public RelayCommand FetchDepartmentCommand { get; set; } | |
// Actual command | |
public void FetchDepartments() | |
{ | |
DepartmentList = new ObservableCollection<Department>(DepartmentRepository.GetAllDepartments()); | |
} | |
protected void OnPropertyChanged([CallerMemberName] string propertyName = null) | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null) | |
{ | |
if (EqualityComparer<T>.Default.Equals(field, value)) return false; | |
field = value; | |
OnPropertyChanged(propertyName); | |
return true; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment