1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| namespace WpfNavBar.ViewModel
{
class BaseVM:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
class HomeViewModel:BaseVM
{
}
class EmployeViewModel:BaseVM
{
}
class DepartmentViewModel : BaseVM
{
}
class NavigationViewModel:BaseVM
{
public ICommand HomeCommand { get; set; }
public ICommand EmpCommand { get; set; }
public ICommand DepCommand { get; set; }
private object selectedViewModel;
public object SelectedViewModel
{
get { return selectedViewModel; }
set
{
selectedViewModel = value;
OnPropertyChanged("SelectedViewModel");
}
}
public NavigationViewModel()
{
selectedViewModel =new HomeViewModel();
HomeCommand = new BaseCommand(OpenHome);
EmpCommand = new BaseCommand(OpenEmp);
DepCommand = new BaseCommand(OpenDep);
}
private void OpenHome(object obj)
{
SelectedViewModel = new HomeViewModel();
}
private void OpenEmp(object obj)
{
SelectedViewModel = new EmployeViewModel();
}
private void OpenDep(object obj)
{
SelectedViewModel = new DepartmentViewModel();
}
}
// class command .
class BaseCommand : ICommand
{
private Predicate<object> _canExecute;
private Action<object> _method;
public event EventHandler CanExecuteChanged;
public BaseCommand(Action<object> method)
: this(method, null)
{
_method = method;
}
public BaseCommand(Action<object> method, Predicate<object> canExecute)
{
_method = method;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
}
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_method.Invoke(parameter);
}
}
} |
Partager