1
我試圖做一個屬性,將用戶選擇的項目,每次顯示其值輸入一個不同的文本的實際值。但我的值與問題是它們是帶下劃線和小寫第一個字母的字符串,例如:「naval_tech_school」。所以,我需要的ComboBox
以顯示不同的值,文本看起來像這樣「海軍技術學校」來代替。讓一個ComboBox顯示修改後的文本作爲值輸入,而不是
但是,如果試圖訪問它,值應該保持「naval_tech_school」。
我試圖做一個屬性,將用戶選擇的項目,每次顯示其值輸入一個不同的文本的實際值。但我的值與問題是它們是帶下劃線和小寫第一個字母的字符串,例如:「naval_tech_school」。所以,我需要的ComboBox
以顯示不同的值,文本看起來像這樣「海軍技術學校」來代替。讓一個ComboBox顯示修改後的文本作爲值輸入,而不是
但是,如果試圖訪問它,值應該保持「naval_tech_school」。
如果你只是想改變值(無特殊編輯器),來回兩種格式之間,你只需要一個定義TypeConverter。財產申報是這樣的:
public class MyClass
{
...
[TypeConverter(typeof(MyStringConverter))]
public string MyProp { get; set; }
...
}
這裏是一個樣本類型轉換器:
public class MyStringConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string svalue = value as string;
if (svalue != null)
return RemoveSpaceAndLowerFirst(svalue);
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
string svalue = value as string;
if (svalue != null)
return RemoveUnderscoreAndUpperFirst(svalue);
return base.ConvertTo(context, culture, value, destinationType);
}
private static string RemoveSpaceAndLowerFirst(string s)
{
// do your format conversion here
}
private static string RemoveUnderscoreAndUpperFirst(string s)
{
// do your format conversion here
}
}
什麼是屬性的類型?它可以是一個自定義類嗎? – 2013-04-24 07:57:33