Okay, so I've answered it myself. The code example shows a class with a property that has the values aaa, bbb, ccc available in a dropdown in a property grid, but the code dynamically adds ddd & eee to the list and they can also be selected in the property grid.
I'm posting the solution in case anyone ever finds this thread whilst searching for the same thing.
Just create a new project, add a property grid to the form and paste the code in.
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:
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Dummy_PropertyGrid
{
class MyConverter : TypeConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
List<string> list = (context.Instance as Form1.myProp).MyList;
StandardValuesCollection cols = new
StandardValuesCollection(list);
return cols;
}
}
public partial class Form1 : Form
{
public class myProp
{
string myStr;
[TypeConverter(typeof(MyConverter))]
public string MyItem
{
get { return myStr; }
set { myStr = value; }
}
List<string> list;
[Browsable(false)]
public List<string> MyList
{
get
{
if (list == null)
{
list = new List<string>();
list.Add("aaa");
list.Add("bbb");
list.Add("ccc");
}
return list;
}
}
}
public Form1()
{
InitializeComponent();
myProp obj = new myProp();
obj.MyList.Add("ddd");
obj.MyList.Add("eee");
this.propertyGrid1.SelectedObject = obj;
}
}
}
|