binding - 带有 selectionModelProperty 问题的 BooleanBinding
问题描述
我有一个带有文本字段、组合框和按钮的窗格。我希望该按钮仅在文本字段具有某些值并且组合选择了某些元素时启用,如果没有则禁用。默认情况下,组合没有选择。我试过这个:
button.disableProperty().bind(Bindings.createBooleanBinding(() ->
(combo.getSelectionModel().getSelectedIndex() == -1) ||
textfield.getText().trim().isEmpty(),
textfield.textProperty(),
combo.selectionModelProperty()
));
但不起作用。如果我删除 combo.selectionModelProperty() 按钮根据文本字段内容启用和禁用正确性,那么 combo.selectionModelProperty() 似乎没有检测到组合中的项目更改。有没有其他方法可以监听项目组合的变化?
解决方案
可以替换a 本身的选择模型ComboBox
,但这很不寻常,需要您自己进行替换。您正在尝试收听 的selectedIndex
属性SelectionModel
,因此您应该使用
combo.getSelectionModel().selectedIndexProperty()
或更好地使用该ComboBox.value
属性:
button.disableProperty().bind(Bindings.createBooleanBinding(
() -> (combo.getValue() == null) || textfield.getText().trim().isEmpty(),
textfield.textProperty(),
combo.valueProperty()
));
推荐阅读
- swift - 如何使用自引用 Swift 协议作为泛型类的类型
- sql - 没有任何关系 id 好坏的 SQL Server 数据库
- r - R自动下载的Excel文件加载问题
- javascript - Highcharts datetimex 轴不正确的开始时间和间隔
- string - Groovy - 如何拆分包含多个文件名但其中一些包含空格的字符串?
- tfs - 如何连接到 TFS Visual Studio 代码
- javascript - knex 查询未使用 express.js 给出预期结果
- ruby-on-rails - RSpec:如何在被测试对象的方法中存根?
- awk - awk:警告:在 Mac OSX 终端中运行命令时,转义序列 `\/' 被视为普通 `/' 错误
- mysql - 修改 mysql 查询,使其提供列的总和