kotlin - 如何将 ItemViewModel 绑定(bind)到组合框和整个表单?

标签 kotlin tornadofx

我有类 UserItemViewModel

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() }
}

此外,我还有 ViewFormController

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))
    }

}

我想将用户列表绑定(bind)到表单

  1. 在组合框中我想看到名称,而不是像这样的地址 image
  2. combobox索引发生变化时,我希望看到基于 bool 属性isAdmin绑定(bind)启用textfield("Psw")

最佳答案

这是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/

相关文章:

android - 使用 kotlin 在 Android 单元测试中模拟对象 - any() 给出 null

android - 如果回收器 View 项中的数据为空,则不显示 View 持有者

kotlin - 改变一些数据的某个属性的值,但是tableview没有更新

java - 在java/kotlin中通过inject()将变量传递给类构造函数

kotlin - TornadoFX:允许将项目从 ListView 复制到剪贴板

kotlin - 从 InputStream 读取的挂起函数

kotlin - 将密封类声明为对象的好处

kotlin - 实现两个id接口(interface)时如何解决冲突?

javafx - 将 TextField 绑定(bind)到 ListView 项

user-interface - 如何删除在borderpane上添加的Fragment()?