How to add ComboBox to PropertyGrid / Property Grid - Dynamic List ComboBox


We would like to show dynamic values (on the fly) in a combo box, avoiding hard coded values.

We may load them from a List,DB or text files on runtime.

 #region Collection will display as a ComboBox in PropertyGrid on the fly.
    public class Collection2PropertyConverter : StringConverter
    {
 
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            //true means show a combobox
            return true;
        }
 
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            //true will limit to list. false will show the list, but allow free-form entry
            return true;
        }
 
        public override
            System.ComponentModel.TypeConverter.StandardValuesCollection
            GetStandardValues(ITypeDescriptorContext context)
        {
            List<string> strCollection = new List<string>();
            if (null != ParameterEditorControl._charCollection)
            {
                for (int i = 0; i <= ParameterEditorControl._charCollection.Count - 1; i++)
                {
                    strCollection.Add(ParameterEditorControl._charCollection[i].ToString());
                }
            }
 
            return new StandardValuesCollection(strCollection);
        }
 
    }
 
    public class CharDisplay
    {
        private string _characteristics;
 
        [Browsable(true)]
        [TypeConverter(typeof(Collection2PropertyConverter))]
        public string Characteristics
        {
            //When first loaded set property with the first item in the rule list.
            get
            {
                string displayItem = "";
                if (_characteristics != null)
                {
                    displayItem = _characteristics;
                }
                else
                {
                    if (ParameterEditorControl._charCollection.Count > 0)
                    {
                        displayItem = ParameterEditorControl._charCollection[0];
                    }
                }
 
                return displayItem;
            }
            set { _characteristics = value; }
 
        }
    }
    #endregion Collection will display as a ComboBox in PropertyGrid on the fly.
           
            Then from the application , fill the list and call CharDisplay :
           
                private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
                                    _charCollection = new ObservableCollection<string>();
                                   
                                    int charCount = Listitems.Count;
                for (int i = 0; i <= charCount - 1; i++)
                {
                    _charCollection.Add(Listitems.Name);
                }
                                               
                                    CharDisplay chardispaly = new CharDisplay();
            PropertyGrid.Characteristics = chardispaly.Characteristics;
                        }

Hope it will help you all.

Cheers.