Note that there are some explanatory texts on larger screens.

plurals
  1. POPropertyGrid control and drop-down lists
    text
    copied!<p>I wanted to create a drop-down list as the editor for a property; If I had only strings as entries for the dropdown list, this would work fine (Using a StringConverter). However, when I tried to use a list of objects instead of Strings, this would not work (note how it works for normal comboboxes however!) This was my code:</p> <pre><code> public static List&lt;Bar&gt; barlist; public Form1() { InitializeComponent(); barlist = new List&lt;Bar&gt;(); for (int i = 1; i &lt; 10; i++) { Bar bar = new Bar(); bar.barvalue = "BarObject " + i; barlist.Add(bar); comboBox1.Items.Add(bar); } Foo foo = new Foo(); foo.bar = new Bar(); propertyGrid1.SelectedObject = foo; } public class Foo { Bar mybar; [TypeConverter(typeof(BarConverter))] public Bar bar { get { return mybar; } set { mybar = value; } } } public class Bar { public String barvalue; public override String ToString() { return barvalue; } } class BarConverter : TypeConverter { public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return new StandardValuesCollection(barlist); } } </code></pre> <p>The result (embedded into a form etc) looked like this:</p> <p><img src="https://i.stack.imgur.com/k6YMa.png" alt="enter image description here"></p> <p>Clicking an entry gave me this:</p> <p><img src="https://i.stack.imgur.com/nPknn.png" alt="enter image description here"></p> <p>(Sorry about the german text, I am not sure if I can change that, my VS is english but my OS isnt, the error message is </p> <pre><code> Invalid property value. The object of type "System.String" cannot be converted to type "XMLTest.Form1+Bar". </code></pre> <p>I am pretty sure I can do a workaround for this by defining backwards conversion tools that convert a string back into a Bar object. This would require the keys being different in order to make this work properly. Is there a nicer way of doing this? Why does the comboBox that is embedded into the propertyGrid control use Strings (the normal comboBox has no problems whatsoever dealing with this)</p> <p>On top of that, can I change the position of the middle separator programmatically? I have yet to find that option.</p> <p>Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload