IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

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

    chinahuyong发表于 2016-05-25 09:21:19
    love 0

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

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

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

      效果如下图所示:

     

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

    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 { get; set; }
     
            public string Text2 { get; set; }
     
            public string comboBox1 { get; set; }
     
            public string ProductCategory { get; set; }
     
            public DateTime? DateTime1 { get; set; }
     
            public decimal? MaskText1 { get; set; }
     
            public int? Int1 { get; set; }
     
            public int Enabled1 { get; set; }
     
            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();
            }
        }
    }


      相关文章列表:

       RDIFramework.NET — 基于.NET的快速信息化系统开发框架 — 系列目录


      一路走来数个年头,感谢RDIFramework.NET框架的支持者与使用者,大家可以通过下面的地址了解详情。

          RDIFramework.NET官方网站:http://www.rdiframework.net/

          RDIFramework.NET官方博客:http://blog.rdiframework.net/

          同时需要说明的,以后的所有技术文章以官方网站为准,欢迎大家收藏!

          RDIFramework.NET框架由专业团队长期打造、一直在更新、一直在升级,请放心使用! 

      欢迎关注RDIFramework.NET框架官方公众微信(微信号:rdiframework-net),及时了解最新动态。

      扫描二维码立即关注

    RDIFramework.NET 官方微信






沪ICP备19023445号-2号
友情链接