如何在 Flex 3 中创建双向数据绑定?
我需要将属性绑定到编辑控件,并让该控件将其值写回同一属性。问题是,我在创建控件之前设置源值:
<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);
发生的情况是这样的:
- docLabel 文本字段最终变为空白(等于“”)
- xmlDoc 的@label 属性设置为“”
我想要是这样的:
- docLabel 文本字段应包含“某个标签”,
- xmlDoc 的 @label 属性应仅在 docLabel 的文本属性更改时更改。
如何使用 Flex 3 实现此目的?
编辑
我也尝试过这个:
<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>
结果是一样的。
I need to bind a property to an edit control and have the control write its value back to the same property. The problem is, I'm setting the source value before the control is created:
<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>
I call this like so:
var xmlDoc: XML = <document label="some label" />;
var myPanel: MyPanel = new MyPanel();
myPanel.editedDocument = xmlDoc;
parent.addChild(myPanel);
What happens is this:
- the docLabel text field ends up blank (equal to "")
- the xmlDoc's @label attribute is set to ""
What I want is this:
- the docLabel text field should contain "some label"
- the xmlDoc's @label attribute should change only when the docLabel's text property changes.
How do I accomplish this, using Flex 3?
Edit
I have also tried this:
<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>
The result is the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以尝试在创建类后使用 BindingUtils 以编程方式创建绑定:
http://life.neophi.com/danielr/2007/03/programmatic_bindings。 html
我已经用它的许多变体来解决类似的问题。如果您无法从链接中弄清楚它,请发表评论,我将深入研究我的源代码,看看我能找到什么。
You can try using BindingUtils to programmatically create the binding after the class has been created:
http://life.neophi.com/danielr/2007/03/programmatic_bindings.html
There are many variations of this that I've used to tackle similar problems. If you can't figure it out from the link post a comment and I'll dig through my source code and see what I can find.
查看双向数据绑定。
看一下正文部分:
您需要将其设置两次:
mx:绑定源=“a.property”目的地=“b.property”/>
mx:Binding source="b.property" target="a.property"/>
变为:
Take a look at Two-way data binding.
Take a look at the part of the text:
tag you need to set it twice:
mx:Binding source="a.property" destination="b.property"/>
mx:Binding source="b.property" destination="a.property"/>
which becomes:
在 Flex 3 中,您最好这样做。还不确定是否可以直接绑定到 XML?
相反,做这样的事情:
In Flex 3 you would be better of doing something like this. Also not sure you can bind directly to XML?
Instead do something like this:
我认为双向数据绑定是 Flex 4 中的一个新功能。
I think bidirectional data binding is a new feature in Flex 4.
这直接来自 Adboe http://opensource.adobe.com /wiki/display/flexsdk/Two-way+Data+Binding,而且它也是 Flex 3!
This is straight from Adboe http://opensource.adobe.com/wiki/display/flexsdk/Two-way+Data+Binding, and it's Flex 3 too!
我创建了自定义控件,当给定一个提供程序对象时,该控件以编程方式创建双向绑定,该提供程序对象具有名称与控件 ID 匹配的合适属性。以下是
TextInput
的示例:}
这是我如何在其他组件之一(弹出对话框)中使用它的示例。
data
属性设置为适当模型类的实例,该实例始终是一个哑的[Bindable]
容器。I created custom controls that programmatically create two-way bindings when given a provider object that has a suitable property whose name matches the control's id. Here's an example for a
TextInput
:}
This is an example of how I use it inside one of my other components (a popup dialog). The
data
property is set to an instance of the appropriate model class, which is always a dumb,[Bindable]
container.