javafx:unbind属性而不引用属性

xoefb8l8  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(291)

我有以下代码:

@Override
public void start(Stage stage) throws Exception {
    TextField textField1=new TextField();
    SimpleStringProperty property=new SimpleStringProperty("Initial Value");
    textField1.textProperty().bindBidirectional(property);
    Button but1=new Button("New value");
    Button but2=new Button("Unbind");
    but1.setOnAction((event)->{
        property.set("New Value");
    });
    but2.setOnAction((event)->{
        //THIS LINE 
    });
    VBox vbox=new VBox();
    vbox.getChildren().addAll(textField1,but1,but2);
    Scene scene=new Scene(vbox, 200, 400);
    stage.setScene(scene);
    stage.show();
}

问题是如果我 THIS LINE 替换为

textField1.textProperty().unbind();

解除绑定不起作用。但如果我换成

textField1.textProperty().unbindBidirectional(property);

解除绑定工作。
我有很多房产,不想保留他们的参考资料。是否可以从textfield1或textfield1.textproperty()获取属性?或者,有没有可能在没有参考 property

llycmphe

llycmphe1#

据我所知,没有办法做到这一点。可以将属性双向绑定到任意数量的属性。即使您可以检索到 textField1.textProperty() 是双向绑定的(我很确定没有办法),你怎么知道你想解除哪个绑定?
“我有很多属性,不想保留它们的引用。”只是建议你需要重新考虑你的设计-你应该只绑定到模型中的东西,你必须保留引用。

相关问题