WPF学习系列 MVVM设计模式 二 简单的增删改

MVVM模式下一个简单的增删改示例。其中还存在部分的问题,会逐步修改。但是增删改已经实现

先看一下项目分层。


对上一节中的代码进行了修改。

Model层中的Student.cs

 public class Student:INotifyPropertyChanged
    {
        private int studentID;
        private string studentName;
        private double studentScore;
        public event PropertyChangedEventHandler PropertyChanged;
        public int StudentID
        {
            get { return studentID; }
            set 
            {
                studentID = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("StudentID"));
            }
        }
        public string StudentName
        {
            get { return studentName; }
            set
            {
                studentName = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("StudentName"));
            }
        }
        public double StudentScore
        {
            get { return studentScore; }
            set
            {
                studentScore = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("StudentScore"));
            }
        }

Model层中的Students.cs

 public class Students
    {
       private static ObservableCollection studentList = null;
       public static ObservableCollection SetStudentList()
       {
           studentList = new ObservableCollection()
           {
                new Student{StudentID = 1,StudentName="A",StudentScore = 90.5},
               new Student{StudentID = 2,StudentName="B",StudentScore = 98.6},
               new Student{StudentID = 3,StudentName="C",StudentScore = 83.2},
               new Student{StudentID = 4,StudentName="D",StudentScore = 71.5},
           };
           return studentList;
       }
       public static void AddStudent(Student student)
       {
           studentList.Add(student);
       }
       public static void UpdateStudent(Student student)
       {
           for (int i = 0; i < studentList.Count; i++)
           {
               if (studentList[i].StudentID == student.StudentID)
               {
                   studentList[i].StudentName = student.StudentName;
                   studentList[i].StudentScore = student.StudentScore;
               }
           }
       }
       public static void DeleteStudent(int studentID)
       {
           for (int i = 0; i < studentList.Count; i++)
           {
               if (studentList[i].StudentID == studentID)
               {
                   studentList.RemoveAt(i);
               }
           }
       }

明显的在项目中多了一层MVVMCommands。其中类实现了ICommand接口。

MVVMCommands层中的AddStudentCommand.cs

  public class AddStudentCommand : ICommand
    {
       private StudentViewModel studentViewModel;
       public AddStudentCommand(StudentViewModel _studentViewModel)
       {
           studentViewModel = _studentViewModel;
       }
       public bool CanExecute(object parameter)
       {
           return true;
       }


       public event EventHandler CanExecuteChanged
       {
           add { }
           remove { }
       }
       public void Execute(object parameter)
       {
           studentViewModel.AddStudents();
       }

MVVMCommands层中的UpdateStudentCommand.cs

 public class UpdateStudentCommand : ICommand
    {
       private StudentViewModel studentViewModel;
       public UpdateStudentCommand(StudentViewModel _studentViewModel)
       {
           studentViewModel = _studentViewModel;
       }
       public bool CanExecute(object parameter)
       {
           return true;
       }


       public event EventHandler CanExecuteChanged
       {
           add { }
           remove { }
       }
       public void Execute(object parameter)
       {
           studentViewModel.UpdateStudents();
       }

MVVMCommands层中的DeleteStudentCommand.cs

  public class DeleteStudentCommand : ICommand
    {
       private StudentViewModel studentViewModel;
       public DeleteStudentCommand(StudentViewModel _studentViewModel)
       {
           studentViewModel = _studentViewModel;
       }
       public bool CanExecute(object parameter)
       {
           return true;
       }


       public event EventHandler CanExecuteChanged
       {
           add { }
           remove { }
       }
       public void Execute(object parameter)
       {
           studentViewModel.DeleteStudents();
       }

将ViewModel层中的StudentViewModel.cs进行行如下修改

   public class StudentViewModel
    {
       private Student student;
       private int id;
       private string name;
       private double score;
       private ICommand addStudentCommand;
       private ICommand upadteStudentCommand;
       private ICommand deleteStudentCommand;
       private ObservableCollection studentListInfo;
     
       public StudentViewModel()
       {
           student = new Student();
           StudentListInfo = Students.SetStudentList();
       }
       public ObservableCollection StudentListInfo
       {
           get { return studentListInfo; }
           set
           {
               studentListInfo = value;
           }
       }
       public int ID
       {
           get { return id; }
           set { id = value; }
       }
       public string Name
       {
           get { return name; }
           set { name = value; }
       }
       public double Score
       {
           get { return score; }
           set { score = value; }
       }


    
       public ICommand AddStudentCommand
       {
           get
           {
               if (addStudentCommand == null)
                   addStudentCommand = new AddStudentCommand(this);
               return addStudentCommand;
           }
       }
       public ICommand UpadteStudentCommand
       {
           get
           {
               if (upadteStudentCommand == null)
                   upadteStudentCommand = new  UpdateStudentCommand(this);
               return upadteStudentCommand;
           }
       }
       public ICommand DeleteStudentCommand
       {
           get
           {
               if (deleteStudentCommand == null)
                   deleteStudentCommand = new  DeleteStudentCommand(this);
               return deleteStudentCommand;
           }
       }
     
       public void AddStudents()
       {
         
           student.StudentID = ID;
           student.StudentName = Name;
           student.StudentScore = Score;
           Students.AddStudent(student);
       }
       public void UpdateStudents()
       {
           student.StudentID = ID;
           student.StudentName = Name;
           student.StudentScore = Score;
           Students.UpdateStudent(student);
       }
       public void DeleteStudents()
       {
           Students.DeleteStudent(ID);
       }

最后看看前台展示的代码

 
       
           
               
               
               
           

       

       

你可能感兴趣的:(WPF)