问题
Is it possible in tornadoFX to bind a ListView to a ListProperty?
I have a ViewModel like follows:
class MyVm: ItemViewModel<Item>() {
val stringProperty = bind { item?.myString?.toProperty() }
}
class MyView: View() {
...
init {
with (root) {
label(myVm.stringProperty)
}
}
}
if the item changes with vm.item = Item(...)
the stringProperty will be updated accordingly, which will update all bound labels etc...
Now I want to do the same with a ListView:
class MyVm: ItemViewModel<Item>() {
val listProperty = bind { item?.myList?.toProperty() }
}
class MyView: View() {
...
init {
with (root) {
listview {
items = myVm.listProperty
}
}
}
}
But in this case the compiler complains that listview.items expects an ObservableList
instead of a ListProperty
回答1:
Define your binding as a ListProperty and pass the listProperty to the listview builder:
val listProperty = bind(Item::myList) as ListProperty<YourType>
..
listview(myVm.listProperty)
来源:https://stackoverflow.com/questions/50260206/tornadofx-bind-listview-to-listproperty