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
public static ObservableCollection
{
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
public StudentViewModel()
{
student = new Student();
StudentListInfo = Students.SetStudentList();
}
public ObservableCollection
{
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);
}
最后看看前台展示的代码
cs代码
public partial class MainWindow : Window
{
private int check = 0;
private StudentViewModel studentViewModel = null;
public MainWindow()
{
InitializeComponent();
studentViewModel = new StudentViewModel();
this.DataContext = studentViewModel;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if(check == 0)
{
if (studentViewModel != null)
studentViewModel.AddStudentCommand.Execute(null);
}
if (check == 1)
{
if (studentViewModel != null)
studentViewModel.UpadteStudentCommand.Execute(null);
}
if(check == 2)
{
if (studentViewModel != null)
studentViewModel.DeleteStudentCommand.Execute(null);
}
}
private void radioButton1_Checked(object sender, RoutedEventArgs e)
{
check = 0;
this.txtID.Text = "";
this.txtName.Text = "";
this.txtScore.Text = "";
this.txtName.IsEnabled = true;
this.txtName.IsEnabled = true;
}
private void radioButton2_Checked(object sender, RoutedEventArgs e)
{
check = 1;
this.txtName.IsEnabled = true;
this.txtScore.IsEnabled = true;
}
private void radioButton3_Checked(object sender, RoutedEventArgs e)
{
check = 2;
this.txtName.IsEnabled = false;
this.txtScore.IsEnabled = false;
}
}
最终效果图: