我有以下代码:
@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
答案 0 :(得分:2)
据我所知,没有办法做到这一点。您可以将属性双向绑定到任意数量的属性。即使你可以检索textField1.textProperty()
双向绑定的属性列表(我很确定没有办法做到这一点),你怎么知道你要解绑哪一个?
“我有很多属性,不想保留他们的参考资料。”只是建议你需要重新考虑你的设计 - 你应该只对你模型中的东西有所约束,你必须保留它的参考。