ASP.net控制項開發系列之七

來源:互聯網
上載者:User

ComponentEditor

“第二選擇”

上篇中,關於Editor說了那麼多,完了嗎?沒有,上篇僅僅介紹了操作屬性的UITypeEditor而已。還記得DataGrid的屬性視窗的下方的“屬性產生器...”嗎?

當我們點擊“屬生產生器...”後,IDE彈出一個表單,提供我們全方位的操作DataGrid屬性的互動介面,這個介面比PropertyGrid提供更方便易用的,更符合DataGrid“國情”。所以,使用者有了屬性窗格之外的第二個選擇。

那麼這個“屬性產生器...”是什麼呢?它也是一個Editor,只是它不是一個UITypeEditor,而是一個ComponentEditor,對,它是一個組件編輯器,它不是用來編輯某個屬性的,而是用來操作整個控制項的。

下面我們就以執行個體來看看要實現組件編輯器,要做哪些工作?

控制項主體類

[
   Designer(typeof(MyDesigner)),
   Editor(typeof(MyComponentEditor), typeof(ComponentEditor))
   ]
   public class MyControl :WebControl {
   }

在這裡我們用到了兩個Attribute來描述我們的控制項主體類:Designer和Editor,第一個為控制項主體類關聯一個設計器,這裡之所以要用到設計器,因為要方便的調用組件編輯器要藉助Designer類。第二個Attribute為控制項主體類關聯了一個編輯器,大家可以看到它的第二個參數變成了ComponentEditor而不是UITypeEditor。

編輯器表單類

public class MyComponentEditorForm : System.Windows.Forms.Form {
     private MyControl _myControl;
     public myControlComponentEditorForm(myControl component) {
       InitializeComponent();
       _myControl = component;
  //用_myControl的屬性初始化本表單上的操作控制項(如一些TextBox,還以我以前講到的PropertyGrid的值)。
     }
//以下是使用者點擊確定完成編輯的邏輯綱要
     private void okButton_Click(object sender, System.EventArgs e) {
這裡使用PropertyDescriptor來為Component賦值與直接用 _myControl.Property_1 = textBox1.Text 這樣的邏輯來賦值有一個好處,就是支援操作的Undo功能#region 這裡使用PropertyDescriptor來為Component賦值與直接用 _myControl.Property_1 = textBox1.Text 這樣的邏輯來賦值有一個好處,就是支援操作的Undo功能
       PropertyDescriptorCollection props = TypeDescriptor.GetProperties(_myControl);
       try {
         PropertyDescriptor property_1 = props["Property_1"];
         if (textProperty != null) {
           textProperty.SetValue(_myControl, textBox1.Text);
         }
       }
       catch {
       }
       DialogResult = DialogResult.OK;
       Close();
#endregion
     }
   }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.