2017-08-02 74 views
0

请看下面的例子:TornadoFX如何将Node样式(或styleClass)绑定到属性?

class MainView : View("Example") { 
    val someBooleanProperty: SimpleBooleanProperty = SimpleBooleanProperty(true) 
    override val root = borderpane { 
     paddingAll = 20.0 
     center = button("Change bg color") { 
      action { 
       // let's assume that new someBooleanProperty value is updated 
       // from some API after button clicked 
       // so changing style of the borderpane in action block 
       // of the button is not the solution 
       someBooleanProperty.value = !someBooleanProperty.value 
      } 
     } 
    } 
} 

class Styles : Stylesheet() { 
    companion object { 
     val red by cssclass() 
     val green by cssclass() 
    } 

    init { 
     red { backgroundColor += Color.RED } 
     green { backgroundColor += Color.GREEN } 
    } 
} 

我怎么能动态改变取决于someBooleanPropertyborderpane背景颜色(如红色时true和绿色时false)?有没有可能将CSS类绑定到一个属性?是否有任何解决方案来做到这一点,而不使用CSS(意思是style块内)

回答

1

如果你想切换一个类(添加或删除基于布尔属性的类),你可以使用Node.toggleClass(CssRule, ObservableValue<Boolean>)函数。

val someBooleanProperty = SimpleBooleanProperty(true) 
... 
borderpane { 
    toggleClass(Styles.red, someBooleanProperty) 
    toggleClass(Styles.green, someBooleanProperty.not()) 
} 

如果,另一方面,你要绑定到一个不断变化的类值,你可以使用Node.bindClass(ObservableValue<CssRule>)功能。

val someClassyProperty = SimpleObjectProperty(Styles.red) 
... 
borderpane { 
    bindClass(someClassyProperty) 
} 

然后,您可以将类设置为任何你想要的。

+0

完美,非常感谢你! – sk1ey

相关问题