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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Windows; using System.Windows.Input;
namespace MVVMTest { public class Student : INotifyPropertyChanged { private string name; public string Name { get => name; set { if (value != name) { name = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name))); } } }
private int grade1; public int Grade1 { get => grade1; set { if (value != grade1) { grade1 = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Grade1))); } } }
private int grade2; public int Grade2 { get => grade2; set { if (value != grade2) { grade2 = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Grade2))); } } }
private int grade3; public int Grade3 { get => grade3; set { if (value != grade3) { grade3 = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Grade3))); } } }
public event PropertyChangedEventHandler PropertyChanged; }
public class ViewModel : INotifyPropertyChanged, IDataErrorInfo { public ViewModel() { Students.Add(new Student { Name = "小明", Grade1 = 80, Grade2 = 90, Grade3 = 100 }); Students.Add(new Student { Name = "小红", Grade1 = 80, Grade2 = 90, Grade3 = 100 }); Students.Add(new Student { Name = "小军", Grade1 = 80, Grade2 = 90, Grade3 = 100 }); }
public event PropertyChangedEventHandler PropertyChanged;
private string name = "小明"; [MaxLength(10, ErrorMessage = "名字长度不能超过10")] public string Name { get => name; set { if (name != value) { name = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name))); } } }
private ObservableCollection<Student> students = new ObservableCollection<Student>(); public ObservableCollection<Student> Students { get => students; set { if (value != students) { students = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Students))); } } }
#region 命令定义 private RelayCommand _showMessageCommand; public RelayCommand ShowMessageCommand => _showMessageCommand ?? (_showMessageCommand = new RelayCommand(ShowMessage));
private RelayCommand _modifyMessageCommand; public RelayCommand ModifyMessageCommand => _modifyMessageCommand ?? (_modifyMessageCommand = new RelayCommand(ModifyMessage));
private RelayCommand _addStudentCommand; public RelayCommand AddStudentCommand => _addStudentCommand ?? (_addStudentCommand = new RelayCommand(AddStudent));
private RelayCommand _deleteStudentCommand; public RelayCommand DeleteStudentCommand => _deleteStudentCommand ?? (_deleteStudentCommand = new RelayCommand(DeleteStudent));
private RelayCommand _modifyStudentCommand; public RelayCommand ModifyStudentCommand => _modifyStudentCommand ?? (_modifyStudentCommand = new RelayCommand(ModifyStudent));
private RelayCommand _windowLoadedCommand; public RelayCommand WindowLoadedCommand => _windowLoadedCommand ?? (_windowLoadedCommand = new RelayCommand(WindowLoaded)); #endregion
#region 数据校验 public string Error => throw new NotImplementedException();
public string this[string columnName] { get { var validationResults = new List<ValidationResult>(); if (Validator.TryValidateProperty( GetType().GetProperty(columnName).GetValue(this), new ValidationContext(this) { MemberName = columnName }, validationResults)) return null; return validationResults.First().ErrorMessage; } } #endregion
#region 业务逻辑 public void ShowMessage(object parameter) { MessageBox.Show(Name); }
public void ModifyMessage(object parameter) { Name = "xiaoming"; }
public void AddStudent(object parameter) { Students.Add(new Student() { Name = "Tom", Grade1 = 60, Grade2 = 60, Grade3 = 60 }); }
public void DeleteStudent(object parameter) { if (Students.Any()) { Students.RemoveAt(0); } }
public void ModifyStudent(object parameter) { if (Students.Any()) { Students[0].Grade1 = 100; } }
public void WindowLoaded(object parameter) { MessageBox.Show("Window Loaded!"); } #endregion }
public class RelayCommand : ICommand { private Action<object> execute; private Func<object, bool> canExecute;
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null) { this.execute = execute; this.canExecute = canExecute; }
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) { if (canExecute != null) return canExecute(parameter); return true; }
public void Execute(object parameter) { execute?.Invoke(parameter); }
public void RaiseCanExecuteChange() { CanExecuteChanged?.Invoke(this, EventArgs.Empty); } } }
|