【问题标题】:Bind ListView to ListProperty将 ListView 绑定到 ListProperty
【发布时间】:2020-01-06 04:32:10
【问题描述】:

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

我有一个如下视图模型:

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

【问题讨论】:

    标签: javafx kotlin tornadofx


    【解决方案1】:

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

    val listProperty = bind(Item::myList) as ListProperty&lt;YourType&gt;

    ..

    listview(myVm.listProperty)

    【讨论】:

    • 完美。我没有意识到 listview 函数参数需要一个 listProperty。所以我尝试分配给项目。
    猜你喜欢
    • 1970-01-01
    • 2016-10-14
    • 2012-05-26
    • 2018-05-21
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多