Adobe Flex 4.5 Spark:将 ItemRenderer 组件绑定到父级

发布于 2024-11-23 14:49:16 字数 2157 浏览 1 评论 0原文

在Flex 3中,过去可以通过outerDocument在itemRenderer中绑定组件属性。例如,如果 itemRenderer 中有一个图像仅在父级的给定条件下显示,则类似这样的内容将完美工作:

<mx:itemRenderer>
 <mx:Component>
   <mx:Label text="{data}"/>
   <mx:Image id="img" visible="{outerDocument.ShowImage}" includeInLayout="{outerDocument.ShowImage}"/>
</mx:Component>
</mx:itemRenderer>

外部文档(不是列表,而是列表所在的 mxml)包含某些内容就像

[Bindable]
public function get ShowImage():void
{
return showImage;
}
public function set ShowImage(val:Boolean):void
{
showImage = val;
}

我尝试在 Flex 4.5 中使用 Spark 项目渲染器使用parentDocument 做同样的事情,但它似乎不知道绑定。当我在 Flex 4.5 中执行此操作时,itemRenderer 似乎不知道parentDocument ShowImage 何时发生更改。

有谁见过这个问题并能够提供解决方案吗?

编辑:添加 Spark 源 按照此处的要求,我的 Spark 源是:

MyItemRenderer.mxml

<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx">
<s:Label id="myLabel" text="{data}/>
<s:Image src="something.png" visible="{parentDocument.ShowImage}" includeInLayout="{parentDocument.ShowImage}"/>
</s:ItemRenderer>

RendererContainer.mxml

<s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[

private var showImage:Boolean = false;

[Bindable]
public function set ShowImage(val:Boolean):void
{
     showImage = val;
}
public function get ShowImage():Boolean
{
     return showImage;
}
]]>
    </fx:Script>
    <!-- Content Group -->
    <s:List id="lstCell" width="100%" height="100%" itemRenderer="MyItemRenderer">      
    </s:List>
</s:Panel>

好的,RendererContainer.mxml 外部的包装器中有一个复选框,用于调度通过更改 Bindable 布尔值来处理的自定义事件。该 var 中的更改随后会更改 RendererContainer 组件上的 ShowImage 属性。我希望 MyItemRenderer 能够拾取绑定,但它似乎不起作用。

所以我的外包装将像这样访问 ShowImage

<comp:RendererContainer id="myId" ShowImage="{myCheckbox.selected}"/>

In Flex 3, it used to be possible to bind a component property within an itemRenderer via outerDocument. So for instance, if there was a image inside an itemRenderer that was only displayed on a given condition of the parent, something like this would work perfectly:

<mx:itemRenderer>
 <mx:Component>
   <mx:Label text="{data}"/>
   <mx:Image id="img" visible="{outerDocument.ShowImage}" includeInLayout="{outerDocument.ShowImage}"/>
</mx:Component>
</mx:itemRenderer>

where the outer document (not the list, but the mxml the list is in) contained something like

[Bindable]
public function get ShowImage():void
{
return showImage;
}
public function set ShowImage(val:Boolean):void
{
showImage = val;
}

I've tried to do the same thing in Flex 4.5 using Spark item renderers using parentDocument, but it doesn't seem to be aware to the binding. When I do this in Flex 4.5, the itemRenderer doesn't seem to be aware when the parentDocument ShowImage changes.

Has anyone seen this issue and is able to offer a solution?

EDIT: Add Spark Source
As requested here is my spark source:

MyItemRenderer.mxml

<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx">
<s:Label id="myLabel" text="{data}/>
<s:Image src="something.png" visible="{parentDocument.ShowImage}" includeInLayout="{parentDocument.ShowImage}"/>
</s:ItemRenderer>

RendererContainer.mxml

<s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[

private var showImage:Boolean = false;

[Bindable]
public function set ShowImage(val:Boolean):void
{
     showImage = val;
}
public function get ShowImage():Boolean
{
     return showImage;
}
]]>
    </fx:Script>
    <!-- Content Group -->
    <s:List id="lstCell" width="100%" height="100%" itemRenderer="MyItemRenderer">      
    </s:List>
</s:Panel>

Ok so there is a checkbox in a wrapper outside of RendererContainer.mxml that dispatches a custom event that is handled by changing a Bindable Boolean. The change in that var then changes the ShowImage property on my RendererContainer component. I would expect that the binding would then be picked up by MyItemRenderer but it doesnt seem to be working.

So my outer wrapper would access ShowImage like this

<comp:RendererContainer id="myId" ShowImage="{myCheckbox.selected}"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不离久伴 2024-11-30 14:49:16

我认为这应该对你有用, YourTypeHere 将是包含的类
对象,请确保 ShowImage 属性是公共且可绑定的。

<mx:itemRenderer>
     <mx:Component> 
     <mx:Script>
        <![CDATA[ 
            import YourTypeHere;
        ]]>
    </mx:Script>
       <mx:Label text="{data}"/>
       <mx:Image id="img" 
        visible="{YourTypeHere(this.parent.ShowImage)}" 
        includeInLayout="{YourTypeHere(this.parent.ShowImage)}"/>
    </mx:Component>
</mx:itemRenderer>

PS 请不要以大写字母开头的属性命名,包括吸气剂,请考虑将其命名为 showImage 并将您的私有变量命名为 _showImage 之类的名称:D

I think this should do the trick for you, YourTypeHere would be the class of the containing
object, make sure the ShowImage property is public and bindable.

<mx:itemRenderer>
     <mx:Component> 
     <mx:Script>
        <![CDATA[ 
            import YourTypeHere;
        ]]>
    </mx:Script>
       <mx:Label text="{data}"/>
       <mx:Image id="img" 
        visible="{YourTypeHere(this.parent.ShowImage)}" 
        includeInLayout="{YourTypeHere(this.parent.ShowImage)}"/>
    </mx:Component>
</mx:itemRenderer>

P.s. please don't name properties with a starting uppercase letter, including getters, consider naming it showImage and your private var to something like _showImage instead :D

要走干脆点 2024-11-30 14:49:16

您的 getter 似乎返回类型为 void。将其更改为布尔值

[Bindable]
public function get ShowImage():Boolean
{
return showImage;
}
public function set ShowImage(val:Boolean):void
{
showImage = val;
}

Your getter seems to have return type as void. Change that to Boolean

[Bindable]
public function get ShowImage():Boolean
{
return showImage;
}
public function set ShowImage(val:Boolean):void
{
showImage = val;
}
静谧 2024-11-30 14:49:16

这会有帮助。

<s:Image src="something.png" visible="{RendererContainer(ListSkin(parentDocument).parentDocument).ShowImage}" includeInLayout="{RendererContainer(ListSkin(parentDocument).parentDocument).ShowImage}"/>

This will help.

<s:Image src="something.png" visible="{RendererContainer(ListSkin(parentDocument).parentDocument).ShowImage}" includeInLayout="{RendererContainer(ListSkin(parentDocument).parentDocument).ShowImage}"/>
绾颜 2024-11-30 14:49:16

以下工作完全正常:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" 
             xmlns:s="library://ns.adobe.com/flex/spark" 
             xmlns:mx="library://ns.adobe.com/flex/mx">
        <s:layout>
            <s:VerticalLayout/>
        </s:layout>
        <fx:Script>
            <![CDATA[

                private var showImage:Boolean = false;

                [Bindable]
                public function set ShowImage(val:Boolean):void
                {
                    showImage = val;
                }
                public function get ShowImage():Boolean
                {
                    return showImage;
                }
            ]]>
        </fx:Script>
        <s:CheckBox label="Select" change="{ShowImage = !ShowImage}"/>
        <!-- Content Group -->
        <s:List id="lstCell" width="100%" height="100%" dataProvider="{new ArrayCollection(['A','B'])}">
            <s:itemRenderer>
                <fx:Component>
                    <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                    xmlns:s="library://ns.adobe.com/flex/spark" 
                                    xmlns:mx="library://ns.adobe.com/flex/mx" 
                                    autoDrawBackground="true">
                        <s:layout>
                            <s:HorizontalLayout/>
                        </s:layout>
                        <s:Label id="myLabel" text="{data}"/>
                        <s:Button label="something.png" visible="{outerDocument.ShowImage}" includeInLayout="{outerDocument.ShowImage}"/>
                    </s:ItemRenderer>
                </fx:Component>
            </s:itemRenderer>
        </s:List>
    </s:Panel>

</s:WindowedApplication>

Following works perfectly fine:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" 
             xmlns:s="library://ns.adobe.com/flex/spark" 
             xmlns:mx="library://ns.adobe.com/flex/mx">
        <s:layout>
            <s:VerticalLayout/>
        </s:layout>
        <fx:Script>
            <![CDATA[

                private var showImage:Boolean = false;

                [Bindable]
                public function set ShowImage(val:Boolean):void
                {
                    showImage = val;
                }
                public function get ShowImage():Boolean
                {
                    return showImage;
                }
            ]]>
        </fx:Script>
        <s:CheckBox label="Select" change="{ShowImage = !ShowImage}"/>
        <!-- Content Group -->
        <s:List id="lstCell" width="100%" height="100%" dataProvider="{new ArrayCollection(['A','B'])}">
            <s:itemRenderer>
                <fx:Component>
                    <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                    xmlns:s="library://ns.adobe.com/flex/spark" 
                                    xmlns:mx="library://ns.adobe.com/flex/mx" 
                                    autoDrawBackground="true">
                        <s:layout>
                            <s:HorizontalLayout/>
                        </s:layout>
                        <s:Label id="myLabel" text="{data}"/>
                        <s:Button label="something.png" visible="{outerDocument.ShowImage}" includeInLayout="{outerDocument.ShowImage}"/>
                    </s:ItemRenderer>
                </fx:Component>
            </s:itemRenderer>
        </s:List>
    </s:Panel>

</s:WindowedApplication>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文