populate a listbox/combox in a propertygrid at run-tim

A

asharda

I am writing a WPF application and am using the windows propertygrid.
I need to show a list or combobox of list of values the user can
select for a particular property, but I do not know the values at
compile time and hence cannot use Enum's. I looked up the net but
couldn't find a good example or code that I can use. If anyone has
done this and can share the code with me it will be very helpful. The
combo or list gets populated as the user adds objects to the screen.

Thanks for your time in advance.
 
A

asharda

I am writing a WPF application and am using the windows propertygrid.
I need to show a list or combobox of list of values the user can
select for a particular property, but I do not know the values at
compile time and hence cannot use Enum's. I looked up the net but
couldn't find a good example or code that I can use. If anyone has
done this and can share the code with me it will be very helpful. The
combo or list gets populated as the user adds objects to the screen.

Thanks for your time in advance.

I figured out how to it so thought of posting it here.

Add this above the property you want displayed in the propertygrid
[TypeConverter(typeof(ListTypeConverter))]
Add this code in your main code
ListTypeConverter.SetList(lst); where lst is a generic list of items.
everytime u add an element to lst call the SetList method.
Add this class to your project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace YourSpace
{
public class ListTypeConverter : TypeConverter
{
private static List m_list = new List();
public override bool GetStandardValuesSupported(ITypeDescriptorContext
context)
{
return true;
}

public override bool GetStandardValuesExclusive(ITypeDescriptorContext
context)
{
return true;
}
private StandardValuesCollection GetValues()
{
return new StandardValuesCollection(m_list);
}
public static void SetList(List list)
{
m_list = list;
}

public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
return GetValues();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top