[英]In TornadoFX, how can I make one Property change when other properties are changed?
For example, I want to do this: I have a StringProperty, and it is a combination of an IntegerProperty and anoter StringProperty, like this: 例如,我要这样做:我有一个StringProperty,它是IntegerProperty和注释器StringProperty的组合,如下所示:
How can I "bind" properties so that the C changes when either A or B are changed? 如何“绑定”属性,以便在更改A或B时更改C? (Note that I don't want to react to changes made to C) (请注意,我不想对C所做的更改做出反应)
I tried using stringBinding()
method, but it is unclear to me how this method works exactly, and the documentation about it is unclear. 我尝试使用stringBinding()
方法,但是我不清楚此方法的确切工作方式,也不清楚它的文档。
stringBinding
is what you want. stringBinding
是您想要的。 The first argument will become this
inside the calculation block, and if this
is an observable value, it will be added as a dependency used to calculate when the string binding should be reevaluated. 第一个参数将在计算块内变为this
参数,如果this
是一个可观察的值,则将其添加为用于计算何时应重新评估字符串绑定的依赖项。 The rest of the parameters (only b
here) are added as re-evaluation dependencies as well. 其余参数(此处仅b
)也作为重新评估的依赖项添加。
If you do stringBinding(a, b) { "${a.value} ${b.value}" }
, you have created a string binding that will update whenever either a
or b
changes. 如果执行stringBinding(a, b) { "${a.value} ${b.value}" }
,则创建了一个字符串绑定,该字符串绑定将在a
或b
更改时更新。 Since this
is a
in the above example, you could also have written in stringBinding(a, b) { "${this.value} ${b.value}" }
but I don't think that makes it any clearer :) 由于在上面的示例中this
是a
,因此您也可以用stringBinding(a, b) { "${this.value} ${b.value}" }
但我认为这没有什么更清楚了:)
The reason we decided to make the first element this
is made more apparent by the following example: 究其原因,我们决定以使第一元件this
是通过下面的例子中变得更加明确:
val sizeDescription = bind(items) { "$size elements in list" }
Here you don't need to reference the items
again to get to it's size
property. 在这里,您无需再次引用这些items
即可获得它的size
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.