WinForm 動態設定控制元件屬性
阿新 • • 發佈:2021-08-30
本文用到的是 Winform + Dev 控制元件
主要功能:可以讀取配置(從資料庫或者配置檔案中讀取),通過配置控制前面控制元件的屬性
- 放點控制元件
- 先準備用到的實體
/// <summary> /// 控制元件配置 /// </summary> public class ControlList { /// <summary> /// 控制元件NAME /// </summary> public string ControlName { get; set; } /// <summary> /// 控制元件型別 /// </summary> public string ControlTypeName { get; set; } /// <summary> /// 控制元件預設值 /// </summary> public string ControlDefaultVal { get; set; } /// <summary> /// 控制元件可用 /// </summary> public bool ControlEnable { get; set; } /// <summary> /// 控制元件顯示 /// </summary> public ControlvisibleInfo ControlVisible { get; set; } } public class ControlvisibleInfo { /// <summary> /// 顯示值 /// </summary> public DevExpress.XtraLayout.Utils.LayoutVisibility IsVisible { get; set; } /// <summary> /// 包控制元件的Item /// </summary> public string PatientItem { get; set; } }
- 正文
直如弦,死道邊;曲如鉤,反封侯public partial class Form5 : Form { public Form5() { InitializeComponent(); } private void Form5_Load(object sender, EventArgs e) { BindData(); SetControlsPropertyVal(); } public List<ControlList> listName = new List<ControlList>(); public void SetControlsPropertyVal() { foreach (ControlList item in listName) { Control[] ctrols = layoutControl1.Controls.Find(item.ControlName, true); if (ctrols.Length > 0) { Type t = ctrols[0].GetType(); if (t.Name == item.ControlTypeName && ctrols[0].Name == item.ControlName) { object o = t.GetProperty("EditValue").GetValue(ctrols[0], null); t.GetProperty("EditValue").SetValue(ctrols[0], item.ControlDefaultVal); t.GetProperty("Enabled").SetValue(ctrols[0], item.ControlEnable); if (item.ControlVisible != null) { DevExpress.XtraLayout.BaseLayoutItem obj = layoutControlGroup1.Items.FindByName(item.ControlVisible.PatientItem); if (obj != null) { obj.Visibility = item.ControlVisible.IsVisible; } } } } } } public void BindData() { #region 繫結控制元件 List<SeachLookUpEditDataBind> list = new List<SeachLookUpEditDataBind> { new SeachLookUpEditDataBind { ID = "1", NAME = "張三" }, new SeachLookUpEditDataBind { ID = "2", NAME = "李四" }, new SeachLookUpEditDataBind { ID = "3", NAME = "王五" }, new SeachLookUpEditDataBind { ID = "4", NAME = "趙六" } }; searchLookUpEdit1.Properties.ValueMember = "ID"; searchLookUpEdit1.Properties.DisplayMember = "NAME"; searchLookUpEdit1.Properties.DataSource = list; searchLookUpEdit2.Properties.ValueMember = "ID"; searchLookUpEdit2.Properties.DisplayMember = "NAME"; searchLookUpEdit2.Properties.DataSource = list; #endregion #region 設定控制元件屬性值 listName.Add(new ControlList { ControlName = "textEdit1", ControlTypeName = "TextEdit", ControlDefaultVal = "234", ControlEnable = true, ControlVisible = new ControlvisibleInfo { IsVisible = DevExpress.XtraLayout.Utils.LayoutVisibility.Always, PatientItem = "layoutControlItem2" } }); listName.Add(new ControlList { ControlName = "searchLookUpEdit1", ControlTypeName = "SearchLookUpEdit", ControlDefaultVal = "1", ControlEnable = false, ControlVisible = new ControlvisibleInfo { IsVisible = DevExpress.XtraLayout.Utils.LayoutVisibility.Always, PatientItem = "layoutControlItem1" } }); listName.Add(new ControlList { ControlName = "searchLookUpEdit2", ControlTypeName = "SearchLookUpEdit", ControlDefaultVal = "2", ControlEnable = true, ControlVisible = new ControlvisibleInfo { IsVisible = DevExpress.XtraLayout.Utils.LayoutVisibility.Never, PatientItem = "layoutControlItem3" } }); listName.Add(new ControlList { ControlName = "radioGroup1", ControlTypeName = "RadioGroup", ControlDefaultVal = "1", ControlEnable = true, ControlVisible = new ControlvisibleInfo { IsVisible = DevExpress.XtraLayout.Utils.LayoutVisibility.Always, PatientItem = "layoutControlItem4" } }); #endregion } }