編寫與.NET屬性窗口交互的RAD組件(三)
2024-07-10 13:03:53
供稿:網友
六、 擴展屬性和字符串轉換:typeconverter和屬性窗口
.net屬性窗口最重要的一個特性就是可以顯示嵌套的屬性,這樣就提供了比屬性類別更加細化和更有邏輯的分類。嵌套屬性對于類目顯示和排序顯示都是適用的。這樣可以讓屬性列表更加緊湊。比如我們用帶有子屬性x和y的一個location屬性來代替top和left兩個屬性就更加合理。
圖2.嵌套屬性
不過,如何來決定一個屬性可以展開呢?這些不是由屬性窗口來決定,而是取決于屬性自己的類型。在.net framework中,每一種類型都是和一個typeconverter聯系在一起的。比如boolean和string的typeconverter就不會允許展開。因為讓boolean類型含有子屬性是沒有意義的。
在.net framework中,typeconverter實際上是執行了不少的方法,在屬性窗口中就更多了。正像他的名字所說明的那樣,typeconverter提供了一種動態的從一種類型改變到另一種類型的標準方式。事實上,屬性窗口只和string打交道。所以他就依賴于typeconverter來進行類型之間的轉換(主要是和string類型的轉換)。typeconverter同樣是可以提供擴展性能以及復雜類型來和屬性窗口交互。
比如,看下面這個person類:
[typeconverter(typeof(personconverter))]
public class person
{
private string firstname = "";
private string lastname = "";
private intage = 0;
public int age
{
get
{
return age;
}
set
{
age = value;
}
}
public string firstname
{
get
{
return firstname;
}
set
{
this.firstname = value;
}
}
public string lastname
{
get
{
return lastname;
}
set
{
this.lastname = value;
}
}
}
我們注意到person類被指定了typeconverterattribute特性,typeconverterattribute特性還指定了這個類的類型轉換器(personconverter)。如果沒有指定typeconverterattribute特性,默認使用typeconverter類,對于一些簡單數據類型,比如font point等,typeconverter可以很好地工作,但如果數據類型比較復雜,那么它對類型是轉換可能就不是我們希望的那樣,因此,我們有必要從typeconverter派生自己的類型轉換器,在這里就是personconverter,本例中,我們首先重載了getpropertiessupported和getproperties方法來決定屬性是否可以展開。
internal class personconverter : typeconverter
{
public override propertydescriptorcollection
getproperties(itypedescriptorcontext context,
object value,
attribute[] filter)
{
return typedescriptor.getproperties(value, filter);
}
public override bool getpropertiessupported(
itypedescriptorcontext context)
{
return true;
}
}
在通常情況下,直接使用tpyeconverter進行轉換已經足夠了。簡單的擴展就是從typeconverter直接派生你所要的類型轉換器,更復雜的擴展就需要從expandableobjectconverter派生類型轉換器了?,F在我們修改personconverter來轉換一個person類并且顯示一個字符串。
internal class personconverter : expandableobjectconverter
{
public override bool canconvertfrom(
itypedescriptorcontext context, type t)
{
if (t == typeof(string))
{
return true;
}
return base.canconvertfrom(context, t);
}
public override object convertfrom(
itypedescriptorcontext context,
cultureinfo info,
object value)
{
if (value is string)
{
try
{
string s = (string) value;
// parse the format "last, first (age)"
//
int comma = s.indexof(',');
if (comma != -1)
{
// now that we have the comma, get
// the last name.
string last = s.substring(0, comma);
int paren = s.lastindexof('(');
if (paren != -1 && s.lastindexof(')') == s.length - 1)
{
// pick up the first name
string first = s.substring(comma + 1, paren - comma - 1);
// get the age
int age = int32.parse(
s.substring(paren + 1,
s.length - paren - 2));
person p = new person();
p.age = age;
p.lastname = last.trim();
p.firstname = first.trim();
return p;
}
}
}
catch {}
// if we got this far, complain that we
// couldn't parse the string
//
throw new argumentexception(
"can not convert '" + (string)value +
"' to type person");
}
return base.convertfrom(context, info, value);
}
public override object convertto(
itypedescriptorcontext context,
cultureinfo culture,
object value,
type desttype)
{
if (desttype == typeof(string) && value is person)
{
person p = (person)value;
// simply build the string as "last, first (age)"
return p.lastname + ", " +
p.firstname + " (" + p.age.tostring() + ")";
}
return base.convertto(context, culture, value, desttype);
}
}
現在看看我們的person屬性在指定了personconverter類型轉換器之后,既可以展開,又可以通過兩種方式來操作了:直接修改和使用子屬性。
圖3. 實現展開的typeconverter
要使用上面的代碼,我們就生成一個usercontrol并且寫下如下的代碼:
private person p = new person();
public person person
{
get
{
return p;
}
set
{
this.p = value;
}
}
-----------------------------------
<<<<<<<<<<<<待續>>>>>>>>>>>>