0 Replies Latest reply on May 25, 2011 12:06 AM by JohanVelthuis

    Bug and workaround: not showing custom contextmenu in flex 4.5 sdk

    JohanVelthuis

      Dear all,

       

      I ran into a bug in flex 4.5 that wasn't there in 4.1.

      It took me about an hour to find out the problem, i hope this helps some people.

       

      The problem seens to be with binding a contextmenu:

       

      If one creates a bindable contextmenu en use that on for instance a datagrid, it shows in 4.1, but not in 4.5:

      <mx:DataGrid contextmenu="{myContextMenu}" />

       

      But if one uses: myGrid.contextmenu = myCustomContextMenu; after myCustomContextMenu is created, it works in both SDK's.

       

       

       

      I included a working/non-working example:

       

      testContextMenu.mxml:

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:comp="comp.*" creationComplete="application1_creationCompleteHandler(event)">

       

          <fx:Script>
              <![CDATA[
                  import mx.events.FlexEvent;
         
                  [Bindable]
                  private var myContextMenu:ContextMenu;
                 
                  protected function application1_creationCompleteHandler(event:FlexEvent):void
                  {
                      myContextMenu = new ContextMenu();
                     
                      var myContextMenuItem:ContextMenuItem = new ContextMenuItem("Test contextmenu");
                      myContextMenu.hideBuiltInItems();
                      myContextMenu.customItems = [myContextMenuItem];
                     
                      //this works in Flex 4.5 SDK:
                      //myGrid.contextMenu = myContextMenu;//uncomment this line to check it out
                  }

       

              ]]>
          </fx:Script>
          <!--Line below works in Flex 4.1 SDK, but in Flex 4.5 there probably is a problem with the binding, because myContextMenu is not shown.-->
          <comp:sampleDataGrid id="myGrid" contextMenu="{myContextMenu}" />
      </s:Application>

       

       

      sampleDataGrid.mxml:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:DataGrid xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   creationComplete="datagrid1_creationCompleteHandler(event)"
                   dataProvider="{ac}">
          <fx:Script>
              <![CDATA[
                  import mx.collections.ArrayCollection;
                  import mx.events.FlexEvent;
                 
                  [Bindable]
                  private var ac:ArrayCollection = new ArrayCollection();
                  protected function datagrid1_creationCompleteHandler(event:FlexEvent):void
                  {
                      var obj:Object;
                      obj = new Object();
                      obj.name = "John";
                      obj.number = 1;
                      ac.addItem(obj);
                     
                      obj = new Object();
                      obj.name = "Foo";
                      obj.number = 2;
                      ac.addItem(obj);
                  }

       

              ]]>
          </fx:Script>
      </mx:DataGrid>