作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有课User
和 ItemViewModel
为了它。
class User(name: String, type: Int, isAdmin: Boolean) {
var name by property<String>(name)
fun nameProperty() = getProperty(User::name)
var type by property<Int>(type)
fun typeProperty() = getProperty(User::type)
var isAdmin by property<Boolean>(isAdmin)
fun isAdminProperty() = getProperty(User::isAdmin)
}
class UserModel : ItemViewModel<User>() {
val name = bind { item?.nameProperty() }
val type = bind { item?.typeProperty() }
val isAdmin = bind { item?.isAdminProperty() }
}
View
与
Form
和
Controller
class ChooseUserView : View() {
val ctrl: ChooseUserCtrl by inject()
override val root = form {
fieldset("Choose user") {
field("Name") {
combobox<User> {
items = ctrl.users
selectionModel.select(0)
}
}
field("Psw") {
textfield {
whenVisible {
ctrl.model.isAdmin
}
}
}
}
}
}
class ChooseUserCtrl : Controller() {
val view: ChooseUserView by inject()
val users = FXCollections.observableArrayList<User>()
val model = UserModel()
init {
users.add(User("disp", 1, false))
users.add(User("admin", 2, true))
}
}
combobox
索引正在改变我希望看到 textfield("Psw")
的绑定(bind)启用基于 bool 属性 isAdmin
. 最佳答案
这是tornadofx作者的答案:
干得好:
class User() {
constructor(name: String, type: Int, isAdmin: Boolean): this() {
this.name = name
this.type = type
this.isAdmin = isAdmin
}
val nameProperty = SimpleStringProperty()
var name by nameProperty
val typeProperty = SimpleIntegerProperty()
var type by typeProperty
val isAdminProperty = SimpleBooleanProperty()
var isAdmin by isAdminProperty
val passwordProperty = SimpleStringProperty()
var password by passwordProperty
}
class UserModel(user: User? = null) : ItemViewModel<User>(user) {
val name = bind(User::nameProperty)
val type = bind(User::typeProperty)
val isAdmin = bind(User::isAdminProperty)
val password = bind(User::passwordProperty)
}
class ChooseUserView : View() {
val ctrl: ChooseUserCtrl by inject()
val selectedUser = UserModel(ctrl.users.first())
override val root = form {
fieldset("Choose user") {
field("Name") {
combobox(selectedUser.itemProperty, ctrl.users) {
cellFormat {
text = it.name
}
}
}
field("Psw") {
visibleWhen(selectedUser.isAdmin)
textfield(selectedUser.password)
}
}
}
}
class ChooseUserCtrl : Controller() {
val users = FXCollections.observableArrayList<User>()
init {
users.add(User("disp", 1, false))
users.add(User("admin", 2, true))
}
}
关于kotlin - 如何将 ItemViewModel 绑定(bind)到组合框和整个表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49387111/
我有一个域类 class UserItem(var simpleItem: Item, var id: String, var status: String, var price: Int) {
我有课 data class Patient( val birthNumber: String , val firstName: String , val lastName: S
我正在尝试通过注入(inject)添加模型以查看 val model = PersonModel by inject() 但错误: 'PersonModel' does not have a comp
我有课User和 ItemViewModel为了它。 class User(name: String, type: Int, isAdmin: Boolean) { var name by p
我是一名优秀的程序员,十分优秀!