Winform中控件与模型MVC

在Windows Forms (WinForms) 应用程序开发中,用户控件(UserControl)与模型(Model)的结合使用是一种常见的MVC(Model-View-Controller)模式的体现,能够有效地分离界面表示层与业务逻辑层,从而提高代码的可维护性和复用性。本文将通过一个具体的案例------一个简易的"学生信息管理系统"------来阐述如何在WinForms中利用用户控件与模型的配合,实现界面与数据的有效解耦。

一、定义模型(Model)

首先,我们需要定义一个表示学生信息的模型类。这个模型将承载学生的个人信息,如姓名、年龄、性别等。以下是一个简单的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}
二、创建用户控件(UserControl)

接下来,创建一个名为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}
三、实现控制器(Controller)

在主窗体中,充当控制器的角色,负责实例化模型和用户控件,并处理数据的传递和更新。

复制代码

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
我们可以看到,用户控件与模型的配合使用不仅提高了界面的灵活性和可复用性,同时也清晰地划分了业务逻辑和界面表示的职责,遵循了MVC设计模式的原则。在实际开发中,这种模式尤其适用于那些需要频繁更新和维护的界面组件,能够显著降低代码维护成本,提高开发效率。
相关推荐
专注VB编程开发20年8 小时前
asp.net mvc如何简化控制器逻辑
后端·asp.net·mvc
hstar952712 小时前
三十五、面向对象底层逻辑-Spring MVC中AbstractXlsxStreamingView的设计
java·后端·spring·设计模式·架构·mvc
漫谈网络1 天前
MVC与MVP设计模式对比详解
设计模式·mvc
面朝大海,春不暖,花不开3 天前
Spring Boot MVC自动配置与Web应用开发详解
前端·spring boot·mvc
残*影5 天前
什么是MVC?
java·spring·mvc
保持学习ing6 天前
黑马Java面试笔记之框架篇(Spring、SpringMvc、Springboot)
java·笔记·spring·面试·mvc·mybatis·springboot
超级无敌永恒暴龙战士6 天前
SpringBoot-配置Spring MVC
spring boot·spring·mvc
二进制小甜豆6 天前
Spring MVC
java·后端·spring·mvc
Rocky4017 天前
SpringMVC的注解
spring boot·后端·mvc
杨-羊羊羊7 天前
MVVM、MVC的区别、什么是MVVM
mvc