- Dynamic Property Grid
- PropertyGrid: DropDown Editor
- PropertyGrid: DropDown Property
- PropertyGrid: CheckBoxList Editor
- PropertyGrid: CheckBoxList Property
You may want to add a dropdown to your property grid. If you do then the below code is what you will need to create the editor.
Build the DropDown Editor
- public class DropDownEditor : System.Drawing.Design.UITypeEditor
- {
- private IWindowsFormsEditorService es = null;
- public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
- {
- return UITypeEditorEditStyle.DropDown;
- }
- public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
- {
- if (provider != null)
- {
- // This service is in charge of popping our ListBox.
- es = ((IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)));
- if (es != null && value is DropDownList)
- {
- dynamic property = (DropDownList)value;
- var list = new ListBox();
- list.Click += ListBox_Click;
- if (property.Values != null)
- {
- foreach (object item in property.Values)
- {
- var propertyInfo = item.GetType().GetProperty(property.DisplayMember);
- list.Items.Add(propertyInfo.GetValue(item, null));
- }
- }
- // Drop the list control.
- es.DropDownControl(list);
- if (list.SelectedItem != null && list.SelectedIndices.Count == 1)
- {
- property.SelectedItem = list.SelectedItem;
- value = property;
- }
- }
- }
- return value;
- }
- void ListBox_Click(object sender, EventArgs e)
- {
- if (es != null)
- {
- es.CloseDropDown();
- }
- }
- }