15
Jul
08

Setting a selection disabled color on a ComboBox control’s dropdown menu in Flex

The following example shows how you can set a background color for a selected item in a disabled Flex ComboBox control by setting the selectionDisabledColor style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/15/setting-a-selection-disabled-color-on-a-combobox-controls-dropdown-menu-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.DropdownEvent;

            private function comboBox_open(evt:DropdownEvent):void {
                comboBox.dropdown.enabled = checkBox.selected;
                comboBox.dropdown.setStyle("selectionDisabledColor", colorPicker.selectedColor);
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object label="Four" />
        <mx:Object label="Five" />
        <mx:Object label="Six" />
        <mx:Object label="Seven" />
        <mx:Object label="Eight" />
        <mx:Object label="Nine" />
        <mx:Object label="Ten" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="enabled:">
                <mx:CheckBox id="checkBox"
                        selected="true" />
            </mx:FormItem>
            <mx:FormItem label="selectionDisabledColor:">
                <mx:ColorPicker id="colorPicker" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:ComboBox id="comboBox"
            dataProvider="{arr}"
            open="comboBox_open(event);" />

</mx:Application>

View source is enabled in the following example.

You can also set the selectionDisabledColor style in an external .CSS file or <mx:Style /> block, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/15/setting-a-selection-disabled-color-on-a-combobox-controls-dropdown-menu-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.DropdownEvent;

            private function comboBox_open(evt:DropdownEvent):void {
                comboBox.dropdown.enabled = checkBox.selected;
            }
        ]]>
    </mx:Script>

    <mx:Style>
        ComboBox {
            dropdownStyleName: myComboBoxDropdownStyleName;
        }

        .myComboBoxDropdownStyleName {
            selectionDisabledColor: red;
            fontWeight: normal;
        }
    </mx:Style>

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object label="Four" />
        <mx:Object label="Five" />
        <mx:Object label="Six" />
        <mx:Object label="Seven" />
        <mx:Object label="Eight" />
        <mx:Object label="Nine" />
        <mx:Object label="Ten" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="enabled:">
                <mx:CheckBox id="checkBox"
                        selected="true" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:ComboBox id="comboBox"
            dataProvider="{arr}"
            open="comboBox_open(event);" />

</mx:Application>

8 Responses to “Setting a selection disabled color on a ComboBox control's dropdown menu in Flex”


  1. 1 Klaus Busse Jul 16th, 2008 at 3:11 am

    Cool… is there a way to prevent the short white flash when opening the dropdown?

    Klaus

  2. 2 peterd Jul 16th, 2008 at 7:14 am

    Klaus Busse,

    One workaround is to set the ComboBox control’s openDuration style to 0 so that the ComboBox opens immediately instead of animating down.

    Peter

  3. 3 Mielno Jul 16th, 2008 at 1:44 pm

    Thanks for all. I’ll try it and write something more later. Bye - Mielno

  4. 4 Shahaji S.S. Jul 16th, 2008 at 11:38 pm

    Dear All,
    If you see carefully, there is one bug in this,
    Put your finger on downarrow, click on combobox and immidiately press downarrow, you are able to change the combo value after checkbox state is disabled.

    what will be the solution????
    shahazee

  5. 5 peterd Jul 17th, 2008 at 12:17 am

    Shahaji S.S.,

    Yeah, I noticed this shortly after I posted the example. The solution is to disable keyboard navigation for the Flex ComboBox control. And as luck would have it, that was the subject of the very next blog post, “Disabling keyboard navigation on the ComboBox control in Flex”.

    Good catch!

    Peter

  6. 6 meir Aug 26th, 2008 at 1:09 pm

    Do you guys know how will it be possible to have a dropdown menu with checkbox next to each option in the dropdown?

  7. 7 peterd Aug 26th, 2008 at 1:44 pm

    meir,

    In order to do that, you’ll need to define an item renderer. For more information, and lots of examples, check out the item renderers section of Alex Harui’s blog at http://blogs.adobe.com/aharui/item_renderers/

    Peter

  8. 8 meir Aug 27th, 2008 at 12:31 am

    Thanks so much,

    Meir

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed