Skip to content

Instantly share code, notes, and snippets.

Created August 30, 2016 01:44
Show Gist options
  • Select an option

  • Save anonymous/b3a05e5f283a27b0fa8a8f1bf151a51d to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/b3a05e5f283a27b0fa8a8f1bf151a51d 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 { _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 = DepartmentRepository.GetAllDepartments();
}
private void OnPropertyChanged([CallerMemberName] string caller = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(caller));
}
}
protected bool SetField<T>(ref T field, T value, string propertyName)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
// props
private string name;
public string Name
{
get { return name; }
set { SetField(ref name, value, "DepartmentList"); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment