Apache flex 如何在Flex3中创建双向数据绑定?
我需要将属性绑定到编辑控件,并让该控件将其值写回同一属性。问题是,我正在创建控件之前设置源值:Apache flex 如何在Flex3中创建双向数据绑定?,apache-flex,data-binding,flex3,Apache Flex,Data Binding,Flex3,我需要将属性绑定到编辑控件,并让该控件将其值写回同一属性。问题是,我正在创建控件之前设置源值: <mx:Panel> <mx:Script> <![CDATA[ [Bindable] public var editedDocument: XML; ]]> </mx:Script> <mx:TextInput id="docLabel" text="{editedD
<mx:Panel>
<mx:Script>
<![CDATA[
[Bindable] public var editedDocument: XML;
]]>
</mx:Script>
<mx:TextInput id="docLabel" text="{editedDocument.@label}"/>
<mx:Binding source="docLabel.text" destination="editedDocument.@label"/>
</mx:Panel>
我这样称呼它:
var xmlDoc: XML = <document label="some label" />;
var myPanel: MyPanel = new MyPanel();
myPanel.editedDocument = xmlDoc;
parent.addChild(myPanel);
var-xmlDoc:XML=;
var myPanel:myPanel=newmypanel();
myPanel.editedDocument=xmlDoc;
parent.addChild(myPanel);
结果是:
- docLabel文本字段最终为空(等于“”)
- xmlDoc的@label属性设置为“”
- docLabel文本字段应包含“某些标签”
- 只有当docLabel的文本属性更改时,xmlDoc的@label属性才应更改
<mx:Panel>
<mx:Script>
<![CDATA[
[Bindable] public var editedDocument: XML;
]]>
</mx:Script>
<mx:TextInput id="docLabel"/>
<mx:Binding source="editedDocument.@label" destination="docLabel.text"/>
<mx:Binding source="docLabel.text" destination="editedDocument.@label"/>
</mx:Panel>
结果是一样的。我认为双向数据绑定是Flex 4中的一个新功能。您可以尝试在创建类后使用BindingUtils以编程方式创建绑定: 我曾经用过很多不同的方法来解决类似的问题。如果你无法从链接中找到答案,请发表评论,我将深入研究我的源代码,看看能找到什么
private function init():void
{
var xmlDoc: XML = <document label="some label" />;
var myPanel: MyPanel = new MyPanel();
myPanel.editedDocument = xmlDoc;
parent.addChild(myPanel);
BindingUtils.bindProperty(docLabel, "text", editedDocument, "label");
//or maybe it should be one of these, I have not done binding to an XML attribute before
BindingUtils.bindProperty(docLabel, "text", editedDocument, "@label");
BindingUtils.bindProperty(docLabel, "text", editedDocument, "{@label}");
}
private function init():void
{
var xmlDoc:XML=;
var myPanel:myPanel=newmypanel();
myPanel.editedDocument=xmlDoc;
parent.addChild(myPanel);
BindingUtils.bindProperty(docLabel,“text”,editedDocument,“label”);
//或者应该是其中之一,我以前没有绑定到XML属性
BindingUtils.bindProperty(docLabel,“text”,editedDocument,“@label”);
BindingUtils.bindProperty(docLabel,“text”,editedDocument,“{@label}”);
}
看一看。
请看正文部分:
在Flex3中,如果要设置
使用
mx:绑定
标记您需要设置两次:
mx:Binding source=“a.property”destination=“b.property”/>
mx:Binding source=“b.property”destination=“a.property”/>
即:
mx:Binding source="a.property" destination="b.property" twoWay="true"/>
这是直接来自Adboe的,也是Flex3 在Flex3中,您最好这样做。还不确定是否可以直接绑定到XML 相反,你可以这样做:
[Bindable]公共变量tmpString:String;
public var onChange():void{
tmpString=docLabel.text;
//设置XML字符串等。
}
]]>
我创建了自定义控件,当给定一个提供程序对象时,该对象具有名称与控件id匹配的适当属性,可以通过编程方式创建双向绑定。下面是
TextInput
的示例:
public class BoundTextInput extends TextInput
{
// some code omitted for brevity:
// Create getter / setter pair, call invalidateProperties()
// and set internal flag for efficiency
// create bindings in commitProperties:
override protected function commitProperties():void
{
if (this._fProviderChanged) {
this._fProviderChanged = false;
if (null != this._provider && this._provider.hasOwnProperty(this.id) && this._provider[this.id] is String) {
// this is the core bit
BindingUtils.bindProperty(this, "text", this._provider, this.id);
BindingUtils.bindProperty(this._provider, this.id, this, "text");
}
}
// Normally, you call the overridden method first,
// but we want to see the values initialized by the new
// binding right away, so we first create the bindings
// and then commit all inherited properties
super.commitProperties();
}
}
这是一个我如何在其他组件(弹出对话框)中使用它的示例。data
属性被设置为相应模型类的实例,该类始终是一个哑的[Bindable]
容器
<?xml version="1.0" encoding="utf-8"?>
<PopUp xmlns="com.econemon.suite.gui.components.*" xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Form width="100%" height="100%" >
<mx:FormHeading label="Server-URL" />
<mx:FormItem label="URL" >
<!--
If it is available, and of type String, data.urlServer
is bound to the text property of this TextInput
-->
<BoundTextInput id="urlServer" provider="{this.data}"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="OK" click="this.submit(event)" />
</mx:FormItem>
</mx:Form>
</PopUp>
您可能没有阅读该链接。在Flex3中,如果您想使用标记设置双向绑定,您需要设置两次:这会变成:我会说,这太可惜了!正确的。不过,我试过了,但它以错误的顺序进行绑定,从而产生了相同的效果。我知道BindingUtils的一般用法,只是找不到正确的调用顺序,无法使代码按我希望的方式运行。你能提供一个简单的两类代码来完成上面的工作吗?我认为关键是在初始化完成后以编程方式(而不是通过MXML元素)添加BindingUtil。实际上,不是,这是对Flex 4绑定规范的描述。你可以直接绑定到XML。花括号可以包含任何有效的AS表达式。不,双向数据绑定在Flex3中非常可能。建议使用创建双向数据绑定的快捷方式。
<?xml version="1.0" encoding="utf-8"?>
<PopUp xmlns="com.econemon.suite.gui.components.*" xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Form width="100%" height="100%" >
<mx:FormHeading label="Server-URL" />
<mx:FormItem label="URL" >
<!--
If it is available, and of type String, data.urlServer
is bound to the text property of this TextInput
-->
<BoundTextInput id="urlServer" provider="{this.data}"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="OK" click="this.submit(event)" />
</mx:FormItem>
</mx:Form>
</PopUp>