Is there a way to use a List like a selection-combobox in a propertyGrid?
For example, is it possible to have a class like this:
public class Foo
{
[DisplayName(nameof(SelectedBar)),
Browsable(true)]
public Bar SelectedBar { get; set; } = null;
[Browsable(false)]
public List<Bar> Bars { get; set; } = new List<Bar>() { new Bar("Bar0"), new Bar("Bar1"), new Bar("Bar2") };
}
public class Bar
{
public string Name;
public Bar(string name) { Name = name; }
public override string ToString()
{
return Name;
}
}
And in propertyGrid let me select one of the objects in the List of "Bar".
Ok i found the solution for it:
You need a derived converter class with a static object-list variable:
public class ListStringConverter: StringConverter
{
public static List<object> Objects = new List<object>();
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(Objects);
}
}
And add a TypeConverter-Attribute with your converter to your selectedObject variable:
public class Foo
{
public Foo()
{
ObjectListStringConverter.Objects = new List<object>(){ new Bar("Bar0"), new Bar("Bar1"), new Bar("Bar2") };
}
[DisplayName(nameof(SelectedBar)),
Browsable(true),
TypeConverter(typeof(ObjectListStringConverter))]
public Bar SelectedBar { get; set; } = null;
}
public class Bar
{
public string Name;
public Bar(string name) { Name = name; }
public override string ToString()
{
return Name;
}
}
And at some point you need to fill the static object-list variable of your converter-class with your objects you want to have in your list, like i did it in the Foo-Constructor above.
EDIT: The list will show the ToString() method return value of each object you added to the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With