我有一个域名类(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 */
}
saleableItems
和soldAwaitingItems
从Trades
内部更新我想使用
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/