首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

WinForms 統一控制項驗證元件化開發(二):可視化設計時屬性配置

     編輯:關於.NET

元件架構概覽

將上篇的驗證邏輯封裝為 Component,拖曳至表單設計介面即可透過屬性視窗視覺化配置控制項集合與驗證規則。

核心元件:CtrlValidation

public partial class CtrlValidation : Component
{
    public CtrlValidation() { InitializeComponent(); typeCache = new TypeCache(); }
    public CtrlValidation(IContainer container) { container.Add(this); InitializeComponent(); typeCache = new TypeCache(); }

    TypeCache typeCache;
    List ctrlCollect = new List();

    [Description("控制項集合")]
    [Editor(typeof(DropEditor), typeof(UITypeEditor))]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public List CtrlCollect { get => ctrlCollect; set => ctrlCollect = value; }

    // DataSource/DataMember 繫結屬性(略,同標準資料繫結模式)

    #region 公開功能方法
    public bool CheckIsNull() { /* 遍歷 ctrlCollect 呼叫 TypeProcess.CheckNull */ }
    public bool CheckIsFormate() { /* 僅 TextEdit 呼叫 CheckFormate */ }
    public void SetCtrlClear() { ctrlCollect.ForEach(c => GetProcess(c).SetCtrlClear()); }
    public void SetCtrlEnable(bool enable) { ctrlCollect.ForEach(c => GetProcess(c).SetCtrlEnable(enable)); }
    public void SetCtrlDefault() { ctrlCollect.ForEach(c => GetProcess(c).SetCtrlDefault()); }
    public void ClearErrText() { ctrlCollect.ForEach(c => GetProcess(c).ClearErrText()); }
    public void LoadEditRowToText(DataRow row) { /* ... */ }
    public void SetCtrlTextToEditRow(DataRow row) { /* ... */ }
    #endregion

    TypeProcess GetProcess(SetControl ctrl) {
        object[] obj = new object[] { ctrl };
        return TypeFactory.CreateType(ctrl.ControlName.GetType().Name, obj, ctrl.ToString(), typeCache);
    }
}

自訂屬性編輯器:DropEditor

public class DropEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
        return (context != null && context.Instance != null) ? UITypeEditorEditStyle.Modal : base.GetEditStyle(context);
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
        IWindowsFormsEditorService iws = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
        if (iws != null) {
            using (FrmChoose ui = new FrmChoose(context, (List)value)) {
                if (iws.ShowDialog(ui) == DialogResult.OK) {
                    value = ui.Value;
                    context.OnComponentChanged();
                }
            }
        }
        return value;
    }
}

選擇視窗:FrmChoose

  • 包含 GridControl (DevExpress) 顯示控制項清單
  • 內嵌 ComboBoxEdit 選擇資料繫結欄位(需先設定 DataSource/DataMember)
  • 建構函數自動掃描容器內控制項,依 arrType 白名單過濾支援型別
  • 透過反射讀取 DataMember 指定的 DataTable 欄位填充 ComboBox
ArrayList arrType = new ArrayList {
    typeof(DevExpress.XtraEditors.LookUpEdit), typeof(DevExpress.XtraEditors.TextEdit),
    typeof(DevExpress.XtraEditors.DateEdit), typeof(DevExpress.XtraEditors.ComboBoxEdit),
    typeof(RealSailing.UI.Common.UCSysParaslkEdit), typeof(RealSailing.UI.Control.TextBoxEx),
    typeof(RealSailing.UI.Control.MemoEditEx), typeof(RealSailing.UI.Common.UCComboBox),
    typeof(RealSailing.UI.Control.PopuLookUpedit), typeof(RealSailing.UI.Control.UCTextEdit)
};

設計時效果

屬性視窗彈出選擇視窗

拖曳 CtrlValidation 至表單 → 自動載入白名單控制項 → 點擊 CtrlCollect 屬性省略號 → 彈出視窗逐項設定 NullAble、Default、Format、KeyFiled、FiledName → 確定完成 → 程式碼呼叫 8 大功能方法。

關鍵技術點總結

  1. UITypeEditor + IWindowsFormsEditorService 實作模態屬性編輯器
  2. DesignerSerializationVisibility.Content 確保集合內容序列化至設計器代碼
  3. ITypeDescriptorContext 取得設計時容器與元件上下文
  4. 反射動態發現容器內 DataSet 並提取欄位資訊
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved