What's the best way to bind a color to the backgro...
# tornadofx
c
What's the best way to bind a color to the background of a view?
r
You can't, as the view can have any number of backgrounds, and the backgrounds can be any paint, not just colors.
You could instead create a separate color property and bind both the background and your other property to it.
(Though I admit that's easier said than done. It's probably easier to add a listener to the original property that updates the background of the view on change)
c
Yeah, but how would I change the color at that point?
r
What do you mean?
c
@Ruckus "updates the background of the view on change"
How would that look
r
Something like this:
Copy code
colorProperty.addListener { _, _, new ->
    node.background = Background(BackgroundFill(new, null, null))
}
c
Yikes
r
It's a bit ugly due to the issues with background I mentioned above. I think we may have a helper so you could instead use
Copy code
node.background = new?.asBackground()
But I don't remember for sure.
c
What extension is that for?
Like what class
r
I believe it's an extension on
Paint
(so it works on colors), but I don't remember for sure. I'd have to look it up.
c
Appears to be a thing :3
r
I actually have a "converting binding" I've used in the past for this very purpose, but it was a bit of a hack, so I don't want to add it to TornadoFX. It allowed you to use
Copy code
backgroundProperty.bindBidirectional(colorProperty, ColorBackgroundConverter)
c
Oooo
A bit too specific tho
Actually, nah that's kinda cool
r
You can create any converter you want and bind any two properties.
c
Ok =^-^=, thank you for your time ❤️