<pre name="code" class="csharp">public class Student : RaiseBase { private StDataSet.studentRow studentrow; public StDataSet.studentRow Studentrow { get { return studentrow; } } public int StuID { get { return xkzdatarow.stuID; } } public string Sname { get { return studentrow.sname; } set { if (studentrow.sname != value) { studentrow.sname = value; RaisePropertyChanged("Sname"); } } } public int Age { get { return studentrow.age; } set { if (studentrow.age != value) { studentrow.age = value; RaisePropertyChanged("Age"); } } } public bool IsNew { get; set; } public int GetMaxStuID() { object obj = DataRepository.GetInstance().sda.GetMaxStuID(); if (obj == null) { return 1; } else { return int.Parse(obj.ToString()); } } public void SetNewStuID() { studentrow.stuID = GetMaxStuID(); } public static Student GetFromDataRow(StDataSet.studentRow _studentrow) { return new Student() { studentrow = _studentrow, IsNew = false, }; } public Student(bool isNew) { studentrow = DataRepository.GetInstance().tbSudents.NewstudentRow(); IsNew = isNew; <span style="white-space:pre"> </span>studentrow.sname = ""; <span style="white-space:pre"> </span>studentrow.age = 0; } public Student() { } }
public class DataRepository { private static DataRepository _instance; public StDataSet stDataSet; public StDataSetTableAdapters.studentTableAdapter sda; public static DataRepository GetInstance() { if (_instance == null) { _instance = new DataRepository(); } return _instance; } private DataRepository() { this.LoadData(); } public StDataSet.studentDataTable tbStudents { get { return stDataSet.student; } } public IEnumerable<Student> GetStudents() { foreach (StDataSet.studentRow dataRow in tbStudents.Rows) { yield return Student.GetFromDataRow(dataRow); } } public void SaveStudent(Student student) { if (student.IsNew) { AddStudent(student); } else { sda.Update(student.Studentrow); } } private void AddStudent(Student student) { tbStudents.Rows.Add(student.Studentrow); sda.Update(student.Studentrow); } public void DeleteStudent(Student student) { sda.Delete(student.Studentrow); } private void LoadData() { stDataSet = new StDataSet(); sda = new StDataSetTableAdapters.studentTableAdapter(); sda.Fill(stDataSet.student); } private void DisposeData() { stDataSet.student.Dispose(); stDataSet.Dispose(); } public void Exit() { DisposeData(); _instance = null; } } }
public class DisplayStudentViewModel : ViewModelBase { private Student _displayedStudent; public Student DisplayedStudent { get { return _displayedStudent; } set { if (_displayedStudent != value) { _displayedStudent = value; RaisePropertyChanged("DisplayedStudent"); RaisePropertyChanged("IsSelected"); } } } public bool IsSelected { get { return (DisplayedStudent != null); } } private ObservableCollection<Student> students; public ObservableCollection<Student> Students { get { if (students == null) { students = new ObservableCollection<Student>(); foreach (var student in DataRepository.GetInstance().GetStudents()) { students.Add(student); } } return students; } } enum EditModeType { DefaultMode, //非编辑模式 AddNewMode, //添加 EditMode, //修改 BgMode, //变更 ZxMode //注销 } EditModeType _editModeType; private EditModeType editModeType { get { return _editModeType; } set { _editModeType = value; } } private RelayCommand _addNewCommand; private RelayCommand _editCommand; private RelayCommand _updateCommand; private RelayCommand _deleteCommand; private RelayCommand _saveCommand; private RelayCommand _cancelCommand; private RelayCommand _exitCommand; public DisplayStudentViewModel() { editModeType = EditModeType.DefaultMode; } public ICommand AddCommand { get { if (this._addNewCommand == null) { this._addNewCommand = new RelayCommand(AddNew,CanAddNew); } return this._addNewCommand; } } public ICommand EditCommand { get { if (this._editCommand == null) { this._editCommand = new RelayCommand(Edit,CanEdit); } return this._editCommand; } } public ICommand UpdateCommand { get { if (this._updateCommand == null) { this._updateCommand = new RelayCommand(Update,CanUpdate); } return this._updateCommand; } } public ICommand DeleteCommand { get { if (this._deleteCommand == null) { this._deleteCommand = new RelayCommand(Delete,CanDelete); } return this._deleteCommand; } } public ICommand SaveCommand { get { if (this._saveCommand == null) { this._saveCommand = new RelayCommand(Save,CanSave); } return this._saveCommand; } } public ICommand CancelCommand { get { if (this._cancelCommand == null) { this._cancelCommand = new RelayCommand(Cancel,CanCancel); } return this._cancelCommand; } } public ICommand ExitCommand { get { if (this._exitCommand == null) { this._exitCommand = new RelayCommand(Exit, CanExit); } return this._exitCommand; } } void AddNew(object obj) { DisplayedStudent = new Student(true); editModeType = EditModeType.AddNewMode; } bool CanAddNew(object obj) { return (IsDefaultMode()); } void Edit(object obj) { editModeType = EditModeType.EditMode; } bool CanEdit(object obj) { return (IsDefaultMode()) && IsSelected; } void Update(object obj) { editModeType = EditModeType.BgMode; } bool CanUpdate(object obj) { return (IsDefaultMode()) && IsSelected; } void Cancel(object obj) { CancelEdit();//略 editModeType = EditModeType.DefaultMode; } bool CanCancel(object obj) { return !IsDefaultMode(); } void Save(object obj) { if (!SaveStudent()) { return; } editModeType = EditModeType.DefaultMode; } bool SaveStudent() { if (editModeType == EditModeType.AddNewMode) { try { DataRepository.GetInstance().SaveStudent(DisplayedStudent); DisplayedStudent.IsNew = false; DisplayedStudent.SetNewStuID(); Students.Add(DisplayedStudent); return true; } catch { return false; } } else if (editModeType == EditModeType.EditMode) { try { DataRepository.GetInstance().SaveStudent(DisplayedStudent); return true; } catch { return false; } } else if (editModeType == EditModeType.BgMode) { try { DataRepository.GetInstance().SaveStudent(DisplayedStudent);//变更数据保存程序略 return true; } catch { return false; } } else try { DataRepository.GetInstance().DeleteStudent(DisplayedStudent); Students.Remove(DisplayedStudent); return true; } catch { return false; } } bool CanSave(object obj) { return !IsDefaultMode(); } void Delete(object obj) { editModeType = EditModeType.ZxMode; } bool CanDelete(object obj) { return (IsDefaultMode()) && IsSelected; } void Exit(object obj) { DataRepository.GetInstance().Exit(); App.Current.Shutdown(); } bool CanExit(object obj) { return (IsDefaultMode()); } private bool IsDefaultMode() { return editModeType == EditModeType.DefaultMode; } }
时间: 2024-11-05 19:02:59