WinForm开发-界面控件到实体,实体到界面控件自动绑定

 在WinForm开发中,我们是不是为绑定界面控件的数据而每个控件每个控件的赋值?在保存修改时是不是也是每个控件每个控件的赋值到实体中?字段一多,那简直就是噩梦。有没有像Web中那样方便的方法直接就自动映射了呢?现在不用如此繁琐,在RDIFramework.NET开发框架的WinForm部分新增了界面控件到实体,实体到界面控件自动绑定,一句话就搞定了,非常的方便。主要是引用“RDIFramework.WinForm.Utilities.dll”dll文件,再调用下面两个方法即可:

  1、实体到界面控件的自动映射:FormBinding.BindObjectToControls(TestEntity, this);

  2、界面控件到实体的自动映射:FormBinding.BindControlsToObject(TestEntity, this);

  效果如下图所示:

 WinForm开发-界面控件到实体,实体到界面控件自动绑定

  下面给出上面的测试界面的全部代码供大家参考:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
 
namespace RDIFramework.Test
{
    using RDIFramework.Utilities;
    using RDIFramework.WinForm.Utilities;
 
    public partial class FrmFormBindingTest : BaseForm
    {
        ExampleEntity TestEntity = new ExampleEntity();
 
        public FrmFormBindingTest()
        {
            InitializeComponent();
        }
 
        public override void FormOnLoad()
        {
            base.FormOnLoad();
            BindCategory();
            FormBinding.BindObjectToControls(TestEntity, this);
        }
 
        private void BindCategory()
        {
            BasePageLogic.BindCategory(base.UserInfo, ProductCategory, "ProductCategory");
            BasePageLogic.BindCategory(base.UserInfo, comboBox1, "Gender");
        }
 
        private void btnEntityToControl_Click(object sender, EventArgs e)
        {
            FormBinding.BindObjectToControls(TestEntity, this);
        }
 
        private void btnControlToEntity_Click(object sender, EventArgs e)
        {
            FormBinding.BindControlsToObject(TestEntity, this);
            this.richTextBox1.Text = TestEntity.ToString();
        }
 
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
 
    public class ExampleEntity
    {
        public string Text1 { getset; }
 
        public string Text2 { getset; }
 
        public string comboBox1 { getset; }
 
        public string ProductCategory { getset; }
 
        public DateTime? DateTime1 { getset; }
 
        public decimal? MaskText1 { getset; }
 
        public int? Int1 { getset; }
 
        public int Enabled1 { getset; }
 
        public ExampleEntity() {
            Text1 = "ValueText1";
            Text2 = "ValueText2";
            DateTime1 = BusinessLogic.ConvertToDateTime(DateTime.Now.AddDays(-2));
            MaskText1 = BusinessLogic.ConvertToNullableDecimal(12345.12);
            Int1 = 124;
            Enabled1 = 1;
            comboBox1 = "男";
            ProductCategory = "其他";
        }
 
        public override string ToString()
        {
            string returnValue = "Text1: " + Text1 + "\r Text2: " + Text2;
            returnValue += "\r comboBox1:" + comboBox1 + "\r Int1:" + Int1.ToString() + "\r DateTime1:" + DateTime1.ToString() ;
            returnValue += "\r ProductCategory:" + ProductCategory + "\r MaskText1:" + MaskText1.ToString() + "\r Enabled1:" + Enabled1.ToString();
            return returnValue.ToString();
        }
    }
}

  



本文转自yonghu86博客园博客,原文链接:http://www.cnblogs.com/huyong/p/5522799.html,如需转载请自行联系原作者

上一篇:Elasticsearch实战(三)-MacOS下载安装启动Elasticserch、Kibana


下一篇:IDEA 配置 Vue 支持