在VS.NET 中,我們可以很方便地使用屬性視窗來對某個控制項的屬性進行設定,那麼,我們有沒有想過,如果在應用程式中,在對程式中的自訂的屬性進行設定時,顯示一個象屬性視窗一樣的表單,能對其中的屬性方便的設定呢?就象所示的一樣。
答案是完全可以的。我們可以使用微軟提供的property屬性控制項來實現該功能。首先,我們建立一個c#的windows應用程式,之後在工具箱中,滑鼠右鍵點選工具箱(TOOLBOX),在彈出的菜單中選擇“添加/移除項”,如所示:
在彈出的視窗中,選擇.NET Freamwork components視窗,再選擇其中的property grid控制項,點擊選擇就完成了對控制項的加入,如所示:
現在,我們可以開始使用該控制項了。第一步,建立在應用程式中將要展現的欄位屬性為public公有屬性。其中,所有的屬性必須有get和set的方法(如果不設定get方法,則要顯示的屬性不會顯示在屬性控制項中)。為了設定相關的屬性,必須設定下面的一些關於屬性控制項的屬性值,如下表所示:
屬性值 |
含義 |
CategoryAttribute |
該屬性對在Property控制項中的屬性按字母順序進行歸類 |
DescriptionAttribute |
其值為對每個屬性的具體文字描述,將會顯示在property控制項的底部 |
BrowsableAttribute |
該值為是否在property控制項中顯示或者隱藏某個屬性 |
ReadOnlyAttribute |
該值為某個屬性值是否在property控制項中唯讀 |
DefaultValueAttribute |
每個屬性的預設值 |
接下來,我們建立一個使用者類,並且使用屬性控制項,使得可以在屬性控制項框中改變其值。我們先引入相關的命名空間:
using System.ComponentModel; |
之後,建立相關的類,設定有關的屬性,代碼如下:
/// Customer class to be displayed in the property grid /// </summary> /// [DefaultPropertyAttribute("Name")] public class Customer { private string _name; private int _age; private DateTime _dateOfBirth; private string _SSN; private string _address; private string _email; private bool _frequentBuyer; [CategoryAttribute("ID Settings"), DescriptionAttribute("Name of the customer")] public string Name { get { return _name; } set { _name = value; } } [CategoryAttribute("ID Settings"), DescriptionAttribute("Social Security Number of the customer")] public string SSN { get { return _SSN; } set { _SSN = value; } } [CategoryAttribute("ID Settings"), DescriptionAttribute("Address of the customer")] public string Address { get { return _address; } set { _address = value; } } [CategoryAttribute("ID Settings"), DescriptionAttribute("Date of Birth of the Customer (optional)")] public DateTime DateOfBirth { get { return _dateOfBirth; } set { _dateOfBirth = value; } } [CategoryAttribute("ID Settings"), DescriptionAttribute("Age of the customer")] public int Age { get { return _age; } set { _age = value; } } [CategoryAttribute("Marketting Settings"), DescriptionAttribute("If the customer has bought more than 10 times, this is set to true")] public bool FrequentBuyer { get { return _frequentBuyer; } set { _frequentBuyer = value; } } [CategoryAttribute("Marketting Settings"), DescriptionAttribute("Most current e-mail of the customer")] public string Email { get { return _email; } set { _email = value; } } public Customer() { } } |
可以看到,在上面的代碼中,我們對customer類中的屬性進行了設定,如姓名,出生日期,地址等。
接著,我們要為建立的customer類建立一個執行個體,並且將其與屬性控制項綁定。屬性控制項會自動根據類中對屬性的相關設定,從而在介面中顯示有關的屬性,並且還可以進行編輯,比如,可以對生日屬性進行修改,修改時會彈出日曆控制項框,十分方便。代碼如下:
private void Form1_Load(object sender, System.EventArgs e) { //建立bill對象,執行個體化CUSTOMER類 Customer bill = new Customer(); //賦值給屬性 bill.Age = 50; bill.Address = " 114 Maple Drive "; bill.DateOfBirth = Convert.ToDateTime(" 9/14/78"); bill.SSN = "123-345-3566"; bill.Email = “bill@aol.com” bill.Name = "Bill Smith"; //將對象綁定到property控制項中 propertyGrid1.SelectedObject = bill; } |
最後,運行程式,我們就得到了本文一開始圖示的結果了。再來回顧下該程式,其中我們使用了CatrgoryAttribute屬性,定義了id settings和MarketSettings,它們在屬性控制項中以分類的形式出現(注意它們前有個“+”號,點擊可以展開看到其子屬性)。同時,我們每當選擇一個屬性時,在屬性控制項框的下部,會同時顯示該屬性的相關描述。
Property屬性控制項還有很多優點,本文只是對其做了簡單介紹,希望能給讀者啟發。