Flex Menus In Context



There's one point in the Flex livedocs language reference on Context Menus that reads:

"In Flex, only top-level components in the application can have context menus. For example, if a DataGrid control is a child of a TabNavigator or VBox container, the DataGrid control cannot have its own context menu."


Then how would you give sub-components their own context menu?


The way to do this is to track mouse-over and mouse-out events for the sub-component. When the mouse enters the sub-component, you can swap the context menu on your top-level component with the context menu of the sub-component. When the mouse exits the sub-component, you can remove the sub-controls and restore your top-level component's context menu.

This works reasonably well; and I implemented this in ComCenter to give each image its own context menu. After setting this up, I was definitely left wondering why couldn't each control in Flex have its own context menu-since the Flash player is tracking which control should receive mouse events-this would at first glance seem like something that was trivial to implement. (Things are rarely as simple as they appear though-I'm all too familiar with being on the other end and having a user say 'why don't you simply build in this feature' not realizing it represents weeks of work).

As Kevin Hoyt points out here and here, each sub-control can have its own context menu. The documentation appears to be in error here. I've posted comments to the livedocs with pointers to Kevin's blog; so hopefully they'll update that entry.

In the meantime, it was too late for me to get this change into ComCenter 0.5 (which is pretty set in stone at this point), but the simpler context menu code will definitely be in 0.6.


2 comments :

Unknown said...

I have tried setting contextMenu property for different components and subcomponents and everything works well. :)

Anonymous said...

Hey! Thanks for the links!