将数字 属性 绑定到布尔值 属性

Bind number property to boolean property

我在 GridPane 内部使用 TitledPane,我想根据 TitledPane 是否展开来设置网格窗格行约束的 percentHeight。 例如

gridPane {
    columnConstraints(hgrow: 'always')
    rowConstraints(vgrow: 'always')  // first row
    wgridRow1 = rowConstraints(percentHeight: 30) //2nd row
    node(column:0, row:0) // 
    tp = titledPane(column: 0, row: 1) {
        listView(items: ['one', 'two', 'three'])
    }
}

我可以使用绑定来根据带标题的窗格是否展开来设置行约束 percentHeight 吗? 例如

wgridRow1.percentHeightProperty().bind(tp.expandedProperty()).using
{ it ? 30 : -1 }

我可以通过向扩展 属性 添加一个侦听器来解决,但我只是想知道是否可以通过 uni-directional 绑定来完成。

谢谢,

保罗

可以使用BindingsAPI。我不知道 Groovy 语法,但在 JavaFX 中它看起来像

wgridRow1.percentHeightProperty().bind(Bindings
    .when(tp.expandedProperty())
    .then(30)
    .otherwise(-1));