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

自訂設計時編輯器:打造專屬的 PropertyGrid UITypeEditor

     編輯:關於.NET

Visual Studio 的 PropertyGrid 是元件設計時配置屬性的核心介面。內建編輯器僅支援基本型別,若需針對特定領域型別(如角度、漸層色彩軌跡條)提供視覺化編輯體驗,必須實作 UITypeEditor。

案例一:AngleEditor —— 下拉式角度選擇器

核心介面實作:

  • GetEditStyle:回傳 DropDown 模式
  • EditValue:透過 IWindowsFormsEditorService.DropDownControl 顯示自訂 AngleControl(UserControl),接收使用者拖曳/點選回傳的角度值
  • PaintValue:在 PropertyGrid 值欄位直接繪製角度指示圖(紅線+圓心),支援設計時即時預覽

案例二:FlashTrackBar —— 專業級漸層軌跡條

功能豐富的數值編輯器,支援:

  • 雙色線性漸層背景(StartColor/EndColor)
  • 顯示模式切換:百分比 / 實際數值 / 無文字
  • 暗化未選區域(DarkenBy 屬性控制遮罩深度)
  • 拖曳/鍵盤/滑鼠滾輪多種交互方式
  • 最佳化重繪:OptimizedInvalidate 僅重繪數值變化區域與文字區域

編輯器註冊與使用

[Editor(typeof(AngleEditor), typeof(UITypeEditor))]
public double Angle { get; set; }

[Editor(typeof(FlashTrackBarValueEditor), typeof(UITypeEditor))]
public int Value { get; set; }

進階架構:泛化編輯器基類

FlashTrackBarValueEditor 封裝共同邏輯:

  1. 建立編輯器控制項實例
  2. 透過虛擬方法 SetEditorProps 讓衍生類客製化編輯器外觀/範圍
  3. 處理 ValueChanged 事件自動關閉下拉框

衍生類 FlashTrackBarDarkenByEditor 僅需覆寫 SetEditorProps 設定 Min=0, Max=255 即可復用完整邏輯。

設計時除錯技巧

  • 在實驗專案中引用編輯器組件,拖曳目標控制項至設計介面驗證
  • 使用 System.Diagnostics.Debugger.Launch() 在 EditValue 中觸發附加除錯器
  • 注意 PermissionSet(SecurityAction.Demand, Name="FullTrust") 屬性,設計時編輯器需完全信任
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved