TornadoFX将ListView绑定到ListProperty

时间:2018-05-09 18:49:52

标签: javafx kotlin tornadofx

在tornadoFX中是否可以将ListView绑定到ListProperty?

我有一个如下的ViewModel:

class MyVm: ItemViewModel<Item>() {
    val stringProperty = bind { item?.myString?.toProperty() }
}

class MyView: View() {
    ...
    init {
        with (root) {
            label(myVm.stringProperty)
        }
    }
}

如果项目随vm.item = Item(...)更改,则stringProperty将相应更新,这将更新所有绑定标签等...

现在我想用ListView做同样的事情:

class MyVm: ItemViewModel<Item>() {
    val listProperty = bind { item?.myList?.toProperty() }
}

class MyView: View() {
    ...
    init {
        with (root) {
            listview {
                items = myVm.listProperty
            }
        }
    }
}

但在这种情况下,编译器抱怨listview.items需要ObservableList而不是ListProperty

1 个答案:

答案 0 :(得分:3)

将绑定定义为ListProperty并将listProperty传递给listview构建器:

val listProperty = bind(Item::myList) as ListProperty<YourType>

...

listview(myVm.listProperty)