Question

I have been strugling with this for a few hours now and I am simply running around in circles increasing my frustration level.

I have a rather straight forward user control, on this control is a label and a combo box. I have exposed public properties for changing the level text and created a GET/SET for DropDownItems.

The list containing my combo box items is declared as follows:

private List<DropdownItemObject> _dropDownItems = new List<DropdownItemObject>();

This class is the item(s) added to the combo box.

public class DropdownItemObject
{
    public DropdownItemObject()
    {
        Tag = null;
        Object = null;
    }

    public DropdownItemObject(string text, object _object = null, object tag = null)
    {
        Text = text;
        Object = _object;
        Tag = tag;
    }

    public object Tag { get; set; }

    public object Object { get; set; }

    public string Text { get; set; }

}

This is the exposed property in the UserControl. The combo box on the user control is called 'comboPrimary'.

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof (CollectionEditor), typeof (UITypeEditor))]
        public List<DropdownItemObject> DropDownItems
        {
            get
            {
                return _dropDownItems;
            }
            set
            {
                _dropDownItems = value;
                comboPrimary.Items.Clear();

                foreach (DropdownItemObject item in _dropDownItems)
                {
                    comboPrimary.Items.Add(item);
                }
            }
        }

When this control is added to a form, I can use the Collections Editor to add the drop down items. Or I can do it programmatically with this:

uc1.DropDownItems.Add(new DropdownItemObject("ITEM 998"));

But, at run time no items appear in the drop down list.

Was it helpful?

Solution

DropDownItems.Add does not call the DropDownItems setter. You're not setting the value of the property, just calling a method on its current value.

You need to say something like

uc1.DropDownItems = new List<DropdownItemObject>() { 
    new DropdownItemObject("ITEM 998") };

in order to call the setter code.

You could also expose your own Add method on your UserControl class, which would in turn call comboPrimary.Items.Add.

public void Add(DropdownItemObject item) {
   comboPrimary.Items.Add(item);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top