【问题标题】:BooleanBinding with selectionModelProperty problem带有 selectionModelProperty 问题的 BooleanBinding
【发布时间】:2019-03-28 16:07:39
【问题描述】:

我有一个带有文本字段、组合框和按钮的窗格。我希望该按钮仅在文本字段具有某些值并且组合选择了某些元素时启用,如果没有则禁用。默认情况下,组合没有选择。 我试过这个:

button.disableProperty().bind(Bindings.createBooleanBinding(() ->
  (combo.getSelectionModel().getSelectedIndex() == -1) || 
    textfield.getText().trim().isEmpty(),
  textfield.textProperty(),
  combo.selectionModelProperty()
));

但不起作用。如果我删除 combo.selectionModelProperty() 按钮根据文本字段内容启用和禁用正确性,那么 combo.selectionModelProperty() 似乎没有检测到组合中的项目更改。有没有其他方法可以监听物品组合的变化?

【问题讨论】:

    标签: binding javafx-8


    【解决方案1】:

    ComboBox 的选择模型本身可以被替换,但这是不寻常的,需要您自己进行替换。您正在尝试收听SelectionModelselectedIndex 属性,因此您应该使用

    combo.getSelectionModel().selectedIndexProperty()
    

    或更好地使用ComboBox.value 属性:

    button.disableProperty().bind(Bindings.createBooleanBinding(
        () -> (combo.getValue() == null) || textfield.getText().trim().isEmpty(),
        textfield.textProperty(),
        combo.valueProperty()
    ));
    

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 2021-09-05
      • 2017-02-23
      • 2021-01-17
      • 2018-07-13
      • 2017-10-01
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      相关资源
      最近更新 更多