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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace TestPG_Enum
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Je crée mon object qui va gérer mes entrées dans le PropertyGrid
PGElements ValeurSelect = new PGElements();
//J'associe l'objet au propertyGrid pGtest
pGtest.SelectedObject = ValeurSelect;
}
}
/// <summary>
/// La classe de l'objet que l'on veut voir dans le propertyGrid
/// </summary>
public class PGElements
{
private String selectedvalue = "Second";
[TypeConverter(typeof(MainListConverter))] //On travaille avec le type de base String, mais il est lié au Converter MainListConverter
public String TestString
{
get
{
return this.selectedvalue;
}
set
{
if (value.GetType() == typeof(String))
this.selectedvalue = value;
}
}
}
/// <summary>
/// Définition du Converter qui va gérer la correspondance entre un simple String et ma liste de valeur
/// </summary>
public class MainListConverter : System.ComponentModel.TypeConverter
{
public List<String> MainList;
private MainListConverter()
{
//Je rempli arbirtrairement ma liste de valeur (peut etre fait dynamiquement)
MainList = new List<string>();
MainList.Add("First");
MainList.Add("Second");
MainList.Add("Third");
MainList.Add("Fourth");
}
#region fonction du Converter
// Indicates this converter provides a list of standard values.
public override bool GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)
{
return true;
}
// Returns a StandardValuesCollection of standard value objects.
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(System.ComponentModel.ITypeDescriptorContext context)
{
// Passes the local integer array.
StandardValuesCollection svc =
new StandardValuesCollection(MainList);
return svc;
}
// Returns true for a sourceType of string to indicate that
// conversions from string to integer are supported. (The
// GetStandardValues method requires a string to native type
// conversion because the items in the drop-down list are
// translated to string.)
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
{
if (sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context, sourceType);
}
// If the type of the value to convert is string, parses the string
// and returns the integer to set the value of the property to.
// This example first extends the integer array that supplies the
// standard values collection if the user-entered value is not
// already in the array.
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value.GetType() == typeof(string))
{
// Parses the string to get the integer to set to the property.
String newVal = (String)value;
// Tests whether new String is already in the list.
if (!MainList.Contains(newVal))
{
// If the String is not in list, adds it in order.
MainList.Add(newVal);
MainList.Sort();
}
// Returns the String value to assign to the property.
return newVal;
}
else
return base.ConvertFrom(context, culture, value);
}
#endregion
}
} |