Skip to content

Instantly share code, notes, and snippets.

@Suchiman
Forked from anonymous/DataViewModel.cs
Last active August 30, 2016 01:48
Show Gist options
  • Save Suchiman/41fc2331be66d506e485e7fa3e38184c to your computer and use it in GitHub Desktop.
Save Suchiman/41fc2331be66d506e485e7fa3e38184c to your computer and use it in GitHub Desktop.
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