Kotlin 如何将ItemViewModel绑定到组合框和整个表单?
我有类Kotlin 如何将ItemViewModel绑定到组合框和整个表单?,kotlin,tornadofx,Kotlin,Tornadofx,我有类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() = getProper
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() }
}
我想将用户列表绑定到表单so
组合框
索引发生变化时,我希望看到基于布尔属性isAdmin
的textfield(“Psw”)
的绑定启用给你:
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))
}
}
class用户(){
构造函数(名称:String,类型:Int,isAdmin:Boolean):this(){
this.name=name
this.type=type
this.isAdmin=isAdmin
}
val nameProperty=SimpleStringProperty()
按名称属性的变量名称
val typeProperty=SimpleIntegerProperty()
按类型属性的变量类型
val isAdminProperty=SimpleBoleAnProperty()
var isAdmin by isAdminProperty
val passwordProperty=SimpleStringProperty()
按passwordProperty设置的var密码
}
类UserModel(用户:用户?=null):ItemViewModel(用户){
val name=bind(用户::nameProperty)
val type=bind(用户::类型属性)
val isAdmin=bind(用户::isAdmin属性)
val password=bind(用户::passwordProperty)
}
类chooseSerview:View(){
val ctrl:chooseServerCtrl by inject()
val selectedUser=UserModel(ctrl.users.first())
覆盖val root=form{
字段集(“选择用户”){
字段(“名称”){
组合框(selectedUser.itemProperty,ctrl.users){
蜂窝格式{
text=it.name
}
}
}
字段(“Psw”){
visibleWhen(selectedUser.isAdmin)
文本字段(选择用户密码)
}
}
}
}
类选择器UserCtrl:Controller(){
val users=FXCollections.observatarraylist()
初始化{
users.add(用户(“disp”,1,false))
users.add(用户(“admin”,2,true))
}
}
combobox单元格格式正是我翻译combobox值所需的格式,谢谢!
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))
}
}