为什么我们不能在 JavaFX 中将 属性 作为参数传递给 属性#unbind()?
Why can't we pass a property as argument to Property#unbind() in JavaFX?
让我们考虑 Property
接口的两种方法:
Property#unbind()
Property#unbindBidirectional(Property<T> other)
如我们所见,当我们想要移除双向绑定时,我们可以传递我们想要移除此绑定的 属性。
然而,当我们移除单向绑定时,我们不能传递这样的属性。怎么解释?
单向绑定
单向绑定涉及的方法有bind
、unbind
、isBound
。
了解单向绑定是 一对一1 很重要。这样做是为了保持一致性。考虑一下如果同时允许多个单向绑定会发生什么。如果我们有:
A
→B
A
→ C
A
应该包含什么? B
的值还是C
的值? bind
的合约要求 Property
将 始终 包含 ObservableValue
的值。来自 javafx.beans.property
:
All properties can be bound to ObservableValues of the same type, which means that the property will always contain the same value as the bound ObservableValue.
如果有多个 ObservableValue
需要观察,Property
将无法维护此合同。因此,强制执行一对一1关系。
由于这种一对一1的关系,调用unbind
时不需要传递ObservableValue
。唯一可能的 ObservableValue
是之前通过 bind
.
给出的那个
值得一提的是,在已经绑定的 Property
上调用 bind
会隐式地从之前的 ObservableValue
上解除绑定。至少,这就是标准实现的工作方式。我找不到定义此行为的文档,所以我想一个实现可能会抛出异常。
1.从技术上讲,这是一种 多对一 关系。多个 Property
可以绑定到同一个 ObservableValue
,但是那个 Property
不能绑定到多个 ObservableValue
。然而,我将在答案中留下 一对一,因为我认为它更好地说明了单向和双向绑定之间的区别。
双向绑定
双向绑定涉及的方法有bindBidrectional
和unbindBidirectional
。
对于双向绑定,关系是多对多。它们也独立于单向绑定。来自 bindBidirectional
:
Create a bidirectional binding between this Property and another one. Bidirectional bindings exists independently of unidirectional bindings. So it is possible to add unidirectional binding to a property with bidirectional binding and vice-versa. However, this practice is discouraged.
It is possible to have multiple bidirectional bindings of one Property.
双向绑定允许这种多对多关系,因为它们会导致每个 Property
相互镜像 。如果一个发生变化,另一个就会更新。来自 javafx.beans.property
:
It is also possible to define a bidirectional binding between two properties, so that both properties always contain the same value. If one of the properties changes, the other one will be updated.
这意味着双向绑定不存在单向绑定存在的一致性问题。考虑以下因素:
A
↔ B
↔ C
如果 A
更改,则 B
将更新。因为B
更新了,C
也会也更新。这意味着在任何给定时间,所有属性都具有相同的值。没有歧义。
由于这种多对多关系,解除绑定时需要目标 Property
;绑定 Property
需要知道 它需要从哪个 Property
解除绑定。
让我们考虑 Property
接口的两种方法:
Property#unbind()
Property#unbindBidirectional(Property<T> other)
如我们所见,当我们想要移除双向绑定时,我们可以传递我们想要移除此绑定的 属性。
然而,当我们移除单向绑定时,我们不能传递这样的属性。怎么解释?
单向绑定
单向绑定涉及的方法有bind
、unbind
、isBound
。
了解单向绑定是 一对一1 很重要。这样做是为了保持一致性。考虑一下如果同时允许多个单向绑定会发生什么。如果我们有:
A
→B
A
→ C
A
应该包含什么? B
的值还是C
的值? bind
的合约要求 Property
将 始终 包含 ObservableValue
的值。来自 javafx.beans.property
:
All properties can be bound to ObservableValues of the same type, which means that the property will always contain the same value as the bound ObservableValue.
如果有多个 ObservableValue
需要观察,Property
将无法维护此合同。因此,强制执行一对一1关系。
由于这种一对一1的关系,调用unbind
时不需要传递ObservableValue
。唯一可能的 ObservableValue
是之前通过 bind
.
值得一提的是,在已经绑定的 Property
上调用 bind
会隐式地从之前的 ObservableValue
上解除绑定。至少,这就是标准实现的工作方式。我找不到定义此行为的文档,所以我想一个实现可能会抛出异常。
1.从技术上讲,这是一种 多对一 关系。多个 Property
可以绑定到同一个 ObservableValue
,但是那个 Property
不能绑定到多个 ObservableValue
。然而,我将在答案中留下 一对一,因为我认为它更好地说明了单向和双向绑定之间的区别。
双向绑定
双向绑定涉及的方法有bindBidrectional
和unbindBidirectional
。
对于双向绑定,关系是多对多。它们也独立于单向绑定。来自 bindBidirectional
:
Create a bidirectional binding between this Property and another one. Bidirectional bindings exists independently of unidirectional bindings. So it is possible to add unidirectional binding to a property with bidirectional binding and vice-versa. However, this practice is discouraged.
It is possible to have multiple bidirectional bindings of one Property.
双向绑定允许这种多对多关系,因为它们会导致每个 Property
相互镜像 。如果一个发生变化,另一个就会更新。来自 javafx.beans.property
:
It is also possible to define a bidirectional binding between two properties, so that both properties always contain the same value. If one of the properties changes, the other one will be updated.
这意味着双向绑定不存在单向绑定存在的一致性问题。考虑以下因素:
A
↔ B
↔ C
如果 A
更改,则 B
将更新。因为B
更新了,C
也会也更新。这意味着在任何给定时间,所有属性都具有相同的值。没有歧义。
由于这种多对多关系,解除绑定时需要目标 Property
;绑定 Property
需要知道 它需要从哪个 Property
解除绑定。