我有一个域名类(class)

class UserItem(var simpleItem: Item, var id: String, var status: String, var price: Int) {
    var upperLimit: Int = 0
    var lowerLimit: Int = 0
    /* methods here */
}

和一个存储这些对象的类
class Trades {
    val saleableItems: ArrayList<UserItem> = ArrayList()
    val soldAwaitingItems: ArrayList<UserItem> = ArrayList()

    /* methods */
}
saleableItemssoldAwaitingItemsTrades内部更新

我想使用saleableItems显示TableView,以便可以绑定(bind)数据和 View 而无需将属性转换为TornadoFX属性,因此我将UserItem包装为ItemViewModel
class UserItemModel(uItem: UserItem) : ItemViewModel<UserItem>() {
    val simpleItem = bind { item?.observable(UserItem::simpleItem) }
    val id = bind { item?.observable(UserItem::id) }
    val status = bind { item?.observable(UserItem::status) }
    val price = bind { item?.observable(UserItem::price) }
    val upperLimit = bind { item?.observable(UserItem::upperLimit) }
    val lowerLimit = bind { item?.observable(UserItem::lowerLimit) }

    init {
        item = uItem
    }
}

有什么办法可以将Trades包装为ItemViewModel<Trades>,这样我就可以
class TradesModel : ItemViewModel<Trades>() {
    val saleableItems: ObservableList<UserItemModel>
    val soldAwaitingItems: ObservableList<UserItemModel>
}

然后在View中使用它
class TradesView : View("My View") {
val tradesModel: TradesModel by inject()

    tableview(tradesModel.saleableItems) {
        column("Name", UserItemModel::simpleItem)
        column("Price", UserItemModel::price)
        column("Lower limit", UserItemModel::lowerLimit)
        column("Upper limit", UserItemModel::upperLimit)
    }
}

最佳答案

你尝试过这样的事情吗?

class UserItemModel(uItem: UserItem) : ItemViewModel<UserItem>() {
    // Same way you defined previously.
}

class TradesViewModel {
    val saleableItems: SimpleListProperty<UserItemModel>(FXCollections.observableArrayList())
    val soldAwaitingItems: SimpleListProperty<UserItemModel>(FXCollections.observableArrayList())
}

class TradesView : View("My View") {
    val tradesModel: TradesViewModel by inject()

    init {
        with(root) {
            tableview(tradesModel.saleableItems) {
                column("Name", UserItemModel::simpleItem)
                column("Price", UserItemModel::price)
                column("Lower limit", UserItemModel::lowerLimit)
                column("Upper limit", UserItemModel::upperLimit)
            }
        }
    }
}

此外,如果您的表是只读的,则可以将UserItemModel定义为简单的数据类-无需扩展ItemViewModel或执行属性绑定(bind)。

关于kotlin - 将域类包装到ItemViewModel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45241125/

10-09 00:02