在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屬性控件還有很多優點,本文只是對其做了簡單介紹,希望能給讀者啟發。