在javafxmvvm中使用数据绑定获取textfield的新值

whitzsjs  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(425)

我已将textfield绑定到viewmodel中的stringproperty,但此stringproperty仅获取旧值。
控制器:

@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了:
//控制器

this.filterResultTextField.textProperty().addListener((observable, oldValue, newValue) -> {
   this.applicationViewModel.filter(newValue);
});

//视图模型

public void filter(String value ) {
   var filterString = value) != null ? value.toLowerCase() : null;
  ...
}

我感谢你的帮助。谢谢你。

cu6pst1q

cu6pst1q1#

我发现了问题。侦听器是在设置绑定之前首先创建的。在创建侦听器之前,应该首先设置绑定。

相关问题