我有一个域类
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 */
}
saleableItems
和soldAwaitingItems
已在Trades
我想使用saleableItems
显示TableView
,以便我可以绑定数据和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)
}
}
答案 0 :(得分:1)
你尝试过这样的事吗?
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
或执行属性绑定。