[英]Get new value of TextField using data binding in JavaFX MVVM
我在 ViewModel 中將 TextField 綁定到 StringProperty,但此 StringProperty 僅獲取舊值。
Controller:
@FXML
private TextField filterResultTextField;
---
this.filterResultTextField.textProperty().bindBidirectional(this.applicationViewModel.filterApplicationPropertyDataProperty());
---
this.filterResultTextField.textProperty().addListener((observable, oldValue, newValue) -> {
this.applicationViewModel.filter();
});
視圖模型:
private final StringProperty filterApplicationPropertyData = new SimpleStringProperty();
---
public StringProperty filterApplicationPropertyDataProperty() {
return filterApplicationPropertyData;
}
---
public void filter() {
var filterString = this.getFilterApplicationPropertyData() != null ? this.getFilterApplicationPropertyData().toLowerCase() : null;
...
}
使用上面的代碼, private final StringProperty filterApplicationPropertyData = new SimpleStringProperty();
只會有前一個/舊值,而不是當前值。 我可以改為執行以下操作(它正在工作),但綁定實際上將變得毫無用處,我認為它不再是 MVVM:
// Controller
this.filterResultTextField.textProperty().addListener((observable, oldValue, newValue) -> {
this.applicationViewModel.filter(newValue);
});
// 視圖模型
public void filter(String value ) {
var filterString = value) != null ? value.toLowerCase() : null;
...
}
我感謝您的幫助。 謝謝。
我發現了問題。 在設置綁定之前首先創建了偵聽器。 在創建監聽器之前應該先設置綁定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.