Wednesday, December 5, 2007

Displaying array as DropDown List in Property Grid

Any list of values can be displayed as a drop down list in the property grid.

By creating a custom converter extended from StringConverter(TypeConverter can also be used), we can display the list of values or value collection in a property grid as a dropdown list..

for example, if you are having a User list, which will updated dynamically, create a converter class as below


public sealed class StringListConverter : StringConverter
{

public static string[] value;
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}

public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}

public override TypeConverter.StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
{
return new StandardValuesCollection(value);
}
}

the value for the static variable can be set whenever the user list gets changed..

Now the StringListConverter can be used as

[Browsable(true), Description("List of users available")]
[TypeConverter(typeof(StringListConverter))]
public string UserList
{
get { return userList; }
set { userList = value; }
}

[Browsable(false)]
public string[] AvailableUserList
{
set { StringListConverter.value = value; }
}


the purpose of maintaining 2 list is to dynamically update the user list.

Property Grid Resources

List of Property Grid resources
A complete resource list for developers using the PropertyGrid component

Property grid is one of the most used component. We can customize it according to our need. This reveals the list of resources you need to learn on how to customize the property grid component. There were links to customize property grid using its 'ICustomTypeDescriptor', 'UITypeEditor ', 'TypeConverter', 'Attributes' etc...