FXML:一种将子 width/height 绑定到父 width/height 的优雅方式

FXML: An elegant way to bind a child width/height to the parent's width/height

我正在使用 FXML 和 JavaFX 来开发图形界面。我发现了这种将子节点的 width/height 绑定到其父节点的 width/height 的方法:

<Node fx:id="parent">
    <children>
        <Node prefWidth=${parent.width}></Node>
    <children>
</Node>

但这感觉不雅。我宁愿不必为节点提供 id,这样它的子节点就可以引用它的尺寸。是否有一些方法可以引用节点的 parent ?也许像 prefWidth=${parent.width}?

注意:我知道这可以通过 Java 以编程方式完成,但这不是我想要的。我正在寻找一种在 FXML 中执行此操作的方法。

表达式绑定通过引用 FXMLLoadernamespace 的元素来工作。没有“this”的概念来指代 "the current node"(据我所知)。因此,访问节点父节点的唯一方法是节点本身成为命名空间的一部分:您可以通过在节点本身上设置 fx:id 来实现:

<Pane>
    <children>
        <Node fx:id="node" prefWidth="${node.parent.width}"></Node>
    <children>
</Pane>