PropertyGrid: DropDown Property

This entry is part 3 of 5 in the series C#: Property Grid
(Last Updated On: )

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 property. You will also need the editor.

Build a DropDown Property Type

  1. public class DropDownList : INotifyPropertyChanged
  2. {
  3.       private dynamic _values;
  4.       private dynamic selectedItem;
  5.       public event PropertyChangedEventHandler PropertyChanged;
  6.       public event PropertyValueChangedEventHandler PropertyValueChanged;
  7.  
  8.       public DropDownList(String name, PropertyGrid pg)
  9.       {
  10.             PropertyName = name;
  11.             PG = pg;
  12.       }
  13.  
  14.       private String PropertyName { get; set; }
  15.       private PropertyGrid PG { get; set; }
  16.  
  17.       public dynamic Values
  18.       {
  19.             get
  20.             {
  21.                   return _values;
  22.             }
  23.             set
  24.             {
  25.                   if (value != null)
  26.                         _values = value;
  27.             }
  28.       }
  29.  
  30.       public string ValueMember { get; set; }
  31.       public string DisplayMember { get; set; }
  32.  
  33.       [Browsable(false)]
  34.       public dynamic SelectedItem
  35.       {
  36.             get
  37.             {
  38.                   return selectedItem;
  39.             }
  40.             set
  41.             {
  42.                   String oldValue = selectedItem;
  43.                   selectedItem = value;
  44.  
  45.                   if (PropertyChanged != null)
  46.                         PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
  47.  
  48.                   if (PG != null)
  49.                   {
  50.                         if (PropertyValueChanged != null)
  51.                               PropertyValueChanged(this, new PropertyValueChangedEventArgs(PG.SelectedGridItem, oldValue));
  52.                   }
  53.             }
  54.       }
  55.  
  56.       public override string ToString()
  57.       {
  58.             return SelectedItem;
  59.       }
  60. }
Series Navigation<< PropertyGrid: DropDown EditorPropertyGrid: CheckBoxList Editor >>