1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
using System;
using System.ComponentModel;
namespace WinPropertyGrid
{
public class CaracteristiqueUniteConverter : ExpandableObjectConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string)) return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if(value.GetType() == typeof( string) )
{ try
{
// Exemple properties
string propertyList = value as string;
string[] properties = propertyList.Split(" ".ToCharArray());
return new CaracteristiqueUnite((string)properties[0],double.Parse( properties[1]));
}
catch (Exception)
{
throw new ArgumentException("The arguments were not valid.");
}
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value.GetType() == typeof( CaracteristiqueUnite) )
{
// Convert to String
if (destinationType == typeof(string))
{
CaracteristiqueUnite myhand = value as CaracteristiqueUnite;
return String.Format("{0} {1}", myhand.Valeur.ToString(), myhand.Unite);
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
//peut etre omis valeur par defaut
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return base.CanConvertTo(context, destinationType);
}
// force a nouvelle instance
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
// si GetCreateInstanceSupported renvoie true, alors CreateInstance sera utilisée pour creer une nouvelle a new instance
// chaque fois qu'une sous proprietes du expandable object a change
public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
{
//' Use the dictionary to create a new instance
return new CaracteristiqueUnite((string)propertyValues["Unite"], (double)propertyValues["Valeur"]);
}
}
} |
Partager