ComboBox selectedItem problem

ComboBox is definitely one of the lousiest Flex components.
First, you can’t customise the itemRenderer for the main control (not the drop-down list).
Second, it has very uncertain behaviour with item selection.

Problem 1:
setting ComboBox.selectedItem = null does not do anything. Logically, it should be equivalent to setting selectedIndex=-1. But it does not work.
There is Flex bug registered and it is marked as Fixed in Flex 3 (Released), which is a bullshit. It is still there.

Problem 2:
The whole Flash/Flex framework is event-driven thing. But sometimes developers forget about it. Unfortunately, including the framework designers.
Somehow, someone thought that dataProvider property will be always set first, and selectedItem second. This is wrong. Here is an example how it could be in a real world:

A multi-state Panel has Combobox. The Panel is cached (after creation it is saved to be reused in later calls).

Here is the sequence of events:
1. Already cached Panel is opened up by PopUpManager
2. Panel is initialised with new data
3. selectedItem property is getting initialised first (by data binding)
4. Panel performs the state change
5. Panel in new state initialises ComboBox by setting the dataProvider (by databinding)

Because there are no new dataChange events coming after this point, selectedItem won’t be set.

Touché!

Here is the overridden class which addresses both problems.

package
{
    import mx.controls.ComboBox;

    public class RightComboBox extends ComboBox
    {
        public function RightComboBox ()
        {
            super();
        }

        override public function set selectedItem(value:Object):void
        {
            if (value == null)
            {
                super.selectedIndex = -1;
            }
            else
            {
                super.selectedItem = value;
            }
        }

        override public function set dataProvider(value:Object):void
        {
        var l_savedSelectedItem:Object = super.selectedItem;
            super.dataProvider = value;
            super.selectedItem = l_savedSelectedItem;
        }

    }
}

One Response to “ComboBox selectedItem problem”

  1. I still see “selectedItem” getting executed before “dataProvider” and my “selectedItem” function has logic to loop through dataProvider which is empty when “selectedItem” is executed.

Leave a Reply