赞
踩
在Windows Forms (WinForms) 应用程序开发中,用户控件(UserControl)与模型(Model)的结合使用是一种常见的MVC(Model-View-Controller)模式的体现,能够有效地分离界面表示层与业务逻辑层,从而提高代码的可维护性和复用性。本文将通过一个具体的案例——一个简易的“学生信息管理系统”——来阐述如何在WinForms中利用用户控件与模型的配合,实现界面与数据的有效解耦。
首先,我们需要定义一个表示学生信息的模型类。这个模型将承载学生的个人信息,如姓名、年龄、性别等。以下是一个简单的Student
模型类定义:
Csharp
- 1public class Student
- 2{
- 3 public int ID { get; set; }
- 4 public string Name { get; set; }
- 5 public int Age { get; set; }
- 6 public string Gender { get; set; }
- 7}
接下来,创建一个名为StudentInfoControl
的用户控件,用于展示和编辑学生信息。这个控件将包含几个TextBox用于输入学生的名字、年龄和性别,以及一个Button用于提交修改。
Xml
- 1<uc:StudentInfoControl x:Class="StudentManagementApp.StudentInfoControl"
- 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- 6 xmlns:uc="clr-namespace:StudentManagementApp.Controls"
- 7 mc:Ignorable="d"
- 8 d:DesignHeight="140" d:DesignWidth="400">
- 9 <Grid>
- 10 <Label Content="Name:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="120"/>
- 11 <TextBox x:Name="txtName" HorizontalAlignment="Left" Height="23" Margin="136,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
- 12
- 13 <Label Content="Age:" HorizontalAlignment="Left" Margin="10,40,0,0" VerticalAlignment="Top" Width="120"/>
- 14 <TextBox x:Name="txtAge" HorizontalAlignment="Left" Height="23" Margin="136,40,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
- 15
- 16 <Label Content="Gender:" HorizontalAlignment="Left" Margin="10,70,0,0" VerticalAlignment="Top" Width="120"/>
- 17 <TextBox x:Name="txtGender" HorizontalAlignment="Left" Height="23" Margin="136,70,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
- 18
- 19 <Button x:Name="btnSave" Content="Save" HorizontalAlignment="Left" Margin="10,100,0,0" VerticalAlignment="Top" Width="75" Click="btnSave_Click"/>
- 20 </Grid>
- 21</uc:StudentInfoControl>
在C#代码中,我们需要实现事件处理和数据绑定:
Csharp
- 1public partial class StudentInfoControl : UserControl
- 2{
- 3 private Student _student;
- 4
- 5 public StudentInfoControl()
- 6 {
- 7 InitializeComponent();
- 8 this.Loaded += StudentInfoControl_Loaded;
- 9 }
- 10
- 11 private void StudentInfoControl_Loaded(object sender, RoutedEventArgs e)
- 12 {
- 13 if (_student != null)
- 14 {
- 15 txtName.Text = _student.Name;
- 16 txtAge.Text = _student.Age.ToString();
- 17 txtGender.Text = _student.Gender;
- 18 }
- 19 }
- 20
- 21 private void btnSave_Click(object sender, RoutedEventArgs e)
- 22 {
- 23 if (_student != null)
- 24 {
- 25 _student.Name = txtName.Text;
- 26 _student.Age = int.Parse(txtAge.Text);
- 27 _student.Gender = txtGender.Text;
- 28 // 调用控制器的更新方法
- 29 OnStudentUpdated(_student);
- 30 }
- 31 }
- 32
- 33 public event EventHandler<Student> StudentUpdated;
- 34
- 35 protected virtual void OnStudentUpdated(Student student)
- 36 {
- 37 StudentUpdated?.Invoke(this, student);
- 38 }
- 39
- 40 public void BindStudent(Student student)
- 41 {
- 42 _student = student;
- 43 }
- 44}
在主窗体中,充当控制器的角色,负责实例化模型和用户控件,并处理数据的传递和更新。
Csharp
- 1public partial class MainForm : Form
- 2{
- 3 private Student _currentStudent = new Student();
- 4
- 5 public MainForm()
- 6 {
- 7 InitializeComponent();
- 8 studentInfoControl1.BindStudent(_currentStudent);
- 9 }
- 10
- 11 private void btnSave_Click(object sender, EventArgs e)
- 12 {
- 13 studentInfoControl1.SaveStudent(_currentStudent);
- 14 }
- 15}
- 16
- 17// 在MainForm.cs的设计视图中添加StudentInfoControl
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。