使用 FXML 在用户控件中创建和绑定 DoubleProperty

Create and bind DoubleProperty in user control with FXML

我创建了一个代表 NumPad 键盘的用户控件。各个按钮的宽度和高度我想实现为DoubleProperty这样以后可以配置。

我遇到的问题是我无法将 属性 绑定到 GridPane 列和行的 prefWidthprefHeight 16=] 文件。我设法做到这一点的唯一方法是在控制控制器中建立连接。但我想尽可能避免它。

这是有问题的 FXML 文件。

<fx:root type="javafx.scene.layout.GridPane" xmlns="http://javafx.com/javafx/" xmlns:fx="http://javafx.com/fxml/1">
    <fx:define>
        <SimpleDoubleProperty fx:id="size">
            <value>
                <Double fx:value="50"/>
            </value>
        </SimpleDoubleProperty>
    </fx:define>

    <gridLinesVisible>false</gridLinesVisible>

    <hgap>5</hgap>
    <vgap>5</vgap>

    <Button text="C" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="0" focusTraversable="false"/>
    <Button text="Backspace" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="0" GridPane.columnSpan="2" focusTraversable="false"/>

    <Button text="7" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="1" focusTraversable="false"/>
    <Button text="8" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="1" GridPane.rowIndex="1" focusTraversable="false"/>
    <Button text="9" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="1" focusTraversable="false"/>

    <Button text="4" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="2" focusTraversable="false"/>
    <Button text="5" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="1" GridPane.rowIndex="2" focusTraversable="false"/>
    <Button text="6" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="2" focusTraversable="false"/>

    <Button text="1" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="3" focusTraversable="false"/>
    <Button text="2" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="1" GridPane.rowIndex="3" focusTraversable="false"/>
    <Button text="3" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="3" focusTraversable="false"/>

    <Button text="0" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="4" GridPane.columnSpan="2" focusTraversable="false"/>
    <Button text="." maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="4" focusTraversable="false"/>

    <Button text="E" onAction="#f" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="3" GridPane.rowIndex="2" GridPane.rowSpan="3" focusTraversable="false"/>

    <columnConstraints>
        <ColumnConstraints fx:id="col1" prefWidth="${size.value}"/>
        <ColumnConstraints fx:id="col2" prefWidth="${size.value}"/>
        <ColumnConstraints fx:id="col3" prefWidth="${size.value}"/>
        <ColumnConstraints fx:id="col4" prefWidth="${size.value}"/>
    </columnConstraints>
    <rowConstraints>
        <RowConstraints fx:id="row1" prefHeight="${size.value}"/>
        <RowConstraints fx:id="row2" prefHeight="${size.value}"/>
        <RowConstraints fx:id="row3" prefHeight="${size.value}"/>
        <RowConstraints fx:id="row4" prefHeight="${size.value}"/>
        <RowConstraints fx:id="row5" prefHeight="${size.value}"/>
    </rowConstraints>
</fx:root>

${size.value} 正确初始化控件,但 sizeColumnConstraints#prefWidthProperty / RowConstraints#prefHeightProperty 之间没有绑定。

如果我手动绑定,一切正常。但正如我所说,我想尽可能避免它。

@FXML
private void initialize() {
    col1.prefWidthProperty().bind(size);
    // ...

    row1.prefHeightProperty().bind(size);
    // ...
}

如果我使用 ${size} 我会得到 NumberFormatException: For input string: "DoubleProperty [value: 50.0]"

似乎无法通过表达式绑定直接访问属性。

但是,如果您通过控制器提供对 属性 的访问,则可以使用 ${controller.size} 来创建绑定:

@FXML
private DoubleProperty size;

public final double getSize() {
    return size.get();
}

public final void setSize(double value) {
    size.set(value);
}

public final DoubleProperty sizeProperty() {
    return size;
}
...
<columnConstraints>
    <ColumnConstraints fx:id="col1" prefWidth="${controller.size}"/>
    <ColumnConstraints fx:id="col2" prefWidth="${controller.size}"/>
    <ColumnConstraints fx:id="col3" prefWidth="${controller.size}"/>
    <ColumnConstraints fx:id="col4" prefWidth="${controller.size}"/>
</columnConstraints>
<rowConstraints>
    <RowConstraints fx:id="row1" prefHeight="${controller.size}"/>
    <RowConstraints fx:id="row2" prefHeight="${controller.size}"/>
    <RowConstraints fx:id="row3" prefHeight="${controller.size}"/>
    <RowConstraints fx:id="row4" prefHeight="${controller.size}"/>
    <RowConstraints fx:id="row5" prefHeight="${controller.size}"/>
</rowConstraints>
...