WinForm開發-界面控件到實體,實體到界面控件自動綁定
在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框架官方公眾微信(微信號:guosisoft),及時了解最新動態。
掃描二維碼立即關注

作者:
RDIF
出處:
http://www.rzrgm.cn/huyong/
Email:
406590790@qq.com
QQ:
406590790
微信:
13005007127(同手機號)
框架官網:
http://www.guosisoft.com/
http://www.rdiframework.net/
框架其他博客:
http://blog.csdn.net/chinahuyong
http://www.rzrgm.cn/huyong
國思RDIF開發框架
,
給用戶和開發者最佳的.Net框架平臺方案,為企業快速構建跨平臺、企業級的應用提供強大支持。
關于作者:系統架構師、信息系統項目管理師、DBA。專注于微軟平臺項目架構、管理和企業解決方案,多年項目開發與管理經驗,曾多次組織并開發多個大型項目,在面向對象、面向服務以及數據庫領域有一定的造詣。現主要從事基于
RDIF
框架的技術開發、咨詢工作,主要服務于金融、醫療衛生、鐵路、電信、物流、物聯網、制造、零售等行業。
如有問題或建議,請多多賜教!
本文版權歸作者和CNBLOGS博客共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,如有問題,可以通過微信、郵箱、QQ等聯系我,非常感謝。

浙公網安備 33010602011771號