我有两个单选按钮,我试图绑定到dataProvider的值。它最初会填充该值,但如果我在代码中更改dp则不会执行任何操作。我错过了什么吗?
这是一个工作示例
<?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"
>
<fx:Declarations>
<s:RadioButtonGroup id="oneGroup" selectedValue="{dataProvider.one}"/>
<s:RadioButtonGroup id="twoGroup" selectedValue="{dataProvider.two}"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var dataProvider:Object = {one:"active",two:false};
public function doinit(e:Event):void {
dataProvider.one = dataProvider.one == "inactive" ? "active":"inactive";
dataProvider.two = !dataProvider.two;
result.text = dataProvider.one + ":" + dataProvider.two;
}
]]>
</fx:Script>
<mx:HBox>
<mx:Form>
<mx:FormItem label="One" direction="horizontal" >
<s:RadioButton groupName="oneGroup" value="active" label="Active" />
<s:RadioButton groupName="oneGroup" value="inactive" label="Inactive" />
</mx:FormItem>
<mx:FormItem label="Two" direction="horizontal" >
<s:RadioButton groupName="twoGroup" value="true" label="True" />
<s:RadioButton groupName="twoGroup" value="false" label="False" />
</mx:FormItem>
<s:Button label="Change DP" click="doinit(event)" />
<s:Label id="result" />
</mx:Form>
</mx:HBox>
</s:Application>
答案 0 :(得分:3)
尝试使用以下类来利用数据绑定:
package
{
[Bindable]
public class DP
{
public var one:String;
public var two:Boolean;
public function DP(one:String, two:Boolean)
{
this.one = one;
this.two = two;
}
}
}
所以对于你的样本:
<?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"
>
<fx:Declarations>
<s:RadioButtonGroup id="oneGroup" selectedValue="{dataProvider.one}"/>
<s:RadioButtonGroup id="twoGroup" selectedValue="{dataProvider.two}"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
[Bindable]
public var dataProvider:DP = new DP("active", false);
public function doinit(e:Event):void {
dataProvider.one = "inactive";
dataProvider.two = true;
}
]]>
</fx:Script>
<mx:HBox>
<mx:Form>
<mx:FormItem label="One" direction="horizontal" >
<s:RadioButton group="{oneGroup}" value="active" label="Active" />
<s:RadioButton group="{oneGroup}" value="inactive" label="Inactive" />
</mx:FormItem>
<mx:FormItem label="Two" direction="horizontal" >
<s:RadioButton group="{twoGroup}" value="true" label="True" />
<s:RadioButton group="{twoGroup}" value="false" label="False" />
</mx:FormItem>
<s:Button label="Change DP" click="doinit(event)" />
</mx:Form>
</mx:HBox>
</s:Application>
答案 1 :(得分:0)
fwiw,这是使标准对象可绑定的代码。忽略我对s:上面的标签的评论,不知道为什么那对我不起作用。
<?xml version="1.0" encoding="utf-8"?>
<mx: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">
<fx:Script>
<![CDATA[
[Bindable(event="OneChanged")]
public var dataProvider:Object = {one:"one"};
private function changeOne():void{
switch(dataProvider.one){
case "one":
// property
dataProvider.one="two";
break;
case "two":
// whole value
dataProvider = {one : "one"};
break;
}
dispatchEvent (new Event ("OneChanged"));
}
]]>
</fx:Script>
<s:VGroup>
<s:Label text="{dataProvider.one}" />
<s:Button click="changeOne()" label="Next" />
</s:VGroup>
</mx:Application>