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 } } }
我怎样才能动态改变borderpane
的背景颜色取决于someBooleanProperty
(例如,红色时为true
,绿色时为false
)? 有没有可能将CSS类绑定到一个属性? 有没有任何解决方案,而不使用CSS(这意味着内部style
块等)
如果你想切换一个类(添加或删除一个基于布尔属性的类),你可以使用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) }
然后,您可以将课程设置为任何您想要的。