using CommandPatternSample.Command; using CommandPatternSample.Model; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace CommandPatternSample.ViewModel { internal class EmpViewModel : INotifyPropertyChanged { private Emp _selectedEmp; private ObservableCollection _emps; public event PropertyChangedEventHandler PropertyChanged; public RelayCommand AddEmpCommand { get; set; } public EmpViewModel() { this.Emps = new ObservableCollection(); this.Emps.Add(new Emp() { Name = "Tom", Job = "Sales" }); this.Emps.Add(new Emp() { Name = "Jack", Job = "Clerk" }); this.Emps.Add(new Emp() { Name = "Phinix", Job = "Manager" }); this.Emps.Add(new Emp() { Name = "John", Job = "Sales" }); this.Emps.Add(new Emp() { Name = "Richard", Job = "Cleaning" }); this.AddEmpCommand = new RelayCommand(new Action(AddEmp)); } public Emp SelectedEmp { get { return _selectedEmp; } set { _selectedEmp = value; this.OnPropertyChanged(nameof(SelectedEmp)); } } public ObservableCollection Emps { get { return _emps; } set { _emps = value; this.OnPropertyChanged(nameof(Emps)); } } public void AddEmp(object param) { if (string.IsNullOrEmpty(param.ToString())) return; this.Emps.Add(new Emp { Name = param.ToString(), Job = "New Job" }); } private void OnPropertyChanged([CallerMemberName] string propertyName = null) { if (this.PropertyChanged == null) return; this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }