tags:

views:

131

answers:

2

I want to create an MXML container component that has some of its own chrome -- a standard query display, et al -- and that supports the addition of child components to it. Something a lot like the existing mx:Panel class, which includes a title label, but acts like a plain mx:Box with regards to adding children.

What's the easiest way to do this?

Edit:

To be clear, I want to be able to extend the container using MXML, so the "Multiple visual children" problem is relevant.

+1  A: 

Extend a container and add a title label. Probably the <mx:Canvas/> will work here. Make the title a public var and include a var for the styleName of the label.

Then override the addChild() method so that any child that is added is added instead to the that is your container.

Leave enough space for your title when you position your Box element (i.e., give its y property enough space. If there is no title you may want to reclaim that space.

That's the basics. Customize to your heart's content.

EDITED TO ADD: I do this creating an ActionScript class first, extending the container I am targeting, and I add the "furniture" — items the class will always use, like title in your case — by overriding createChildren and calling super.addChild(item) for those items. Calling addChild from then on, even in MXML markup, adds items to the inner container.

Robusto
I don't believe this works, at least if you want to specify your children in mxml, you'll get the "Multiple sets of visual children have been specified for this container bug".
quoo
@quoo: Of course it works, if you do it right. I do it all the time. You just leave out the `super.addChild()` line in the override. But thanks for the drive-by.
Robusto
Okay, I should clarify, this doesn't work if you define your custom component in MXML with children, and then try to add more mxml children to the component when you create an instance of it. It's not a very practical solution.
quoo
@quoo: Maybe it's not a practical solution for you, but who are you to judge everyone else's needs and environment? The component is defined in an ActionScript class, but it works quite well when used as an MXML manifestation in MXML components.
Robusto
Not quite clear why you're so offended - your original solution implied that you were to use mxml, but didn't work as an mxml component.
quoo
A: 

We do this with states.

We put the chrome for the base container in a state (in mx:AddChild elements) and then use the initialize event to switch to that state when the control is created. All the chrome is then added to the container.

That gets round the multiple sets of visual children problem.

The downsides to this approach are:

  • You don't see the chrome when editing descendents of the base.

  • You can't directly access the parent chrome controls from descendent components as they are not there at compile time (instead, you need to define properties, methods or events on the base that the descendents can access)

However, it works well for us.

DanK