C#控制項開發(三)

來源:互聯網
上載者:User
六、 擴充屬性和字串轉換:TypeConverter和屬性視窗
.NET屬性視窗最重要的一個特性就是可以顯示嵌套的屬性,這樣就提供了比屬性類別更加細化和更有邏輯的分類。嵌套屬性對於類目顯示和排序顯示都是適用的。這樣可以讓屬性列表更加緊湊。比如我們用帶有子屬性X和Y的一個Location屬性來代替Top和Left兩個屬性就更加合理。

不過,如何來決定一個屬性可以展開呢?這些不是由屬性視窗來決定,而是取決於屬性自己的類型。在.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衍生類別型轉換器了。現在我們修改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類型轉換器之後,既可以展開,又可以通過兩種方式來操作了:直接修改和使用子屬性。

要使用上面的代碼,我們就產生一個UserControl並且寫下如下的代碼:

private Person p = new Person();

public Person Person

{

get

{

return p;

}

set

{

this.p = value;

}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.