JavaFX FXML:展开 ScrollPane 以适应其内容窗格的高度,直到它重新达到其 MaxHeight 属性

JavaFX FXML: Expand ScrollPane to Fit height of its Content Pane Until it reahces its MaxHeight property

我有以下 FXML 视图:

我希望 ScrollPane 展开 其高度以匹配其 child vbox's height,直到它达到特定高度 [例如 200px] 然后停止展开。

过去 1 小时我一直在尝试使用 Min/Pref Viewport HeightMin/Max/Pref Height 属性来实现此目的,但没有任何结果。

这可以吗?

编辑:这是一个最小的、完整的、可验证的例子:

MainView.fxml

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.VBox?>

<VBox alignment="CENTER" prefHeight="0.0" prefWidth="200.0" spacing="5.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainController">
   <ScrollPane maxHeight="200.0"  VBox.vgrow="ALWAYS">
         <VBox fx:id="_buttonsContainer" />
   </ScrollPane>
   <Button fx:id="_btnAddButton" mnemonicParsing="false" text="Button" />
</VBox>

MainController.java

public class MainController implements Initializable{

    @FXML private Button _btnAddButton;
    @FXML private VBox   _buttonsContainer;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        _btnAddButton.setOnAction(e -> {
            addButton();
        });
    }

    private void addButton(){
        _buttonsContainer.getChildren().add(new Button());
    }
}

ScrollPane.prefHeightProperty().bind(VBox.heightProperty());

然后添加ScrollPane.setMaxHeight(200);试试这个

这似乎有效:

   <ScrollPane fx:id="scrollPane" maxHeight="200.0" minViewportHeight="0.0" minHeight="0.0">
         <VBox fx:id="_buttonsContainer" />
   </ScrollPane>

默认情况下,滚动窗格及其视口似乎有一些固定的正最小高度。