开发者

WPF "Coloured binding"

开发者 https://www.devze.com 2023-04-08 09:30 出处:网络
I have 开发者_C百科few labels I use for displaying results. Basically, they display numbers in following format

I have 开发者_C百科few labels I use for displaying results.

Basically, they display numbers in following format

string.Format("{0:0.#}", number)

Their Text property is binded to objects. Result should be plus or minus signed.

Is there a way to set Foreground property of label according to result sign? For example green plus results and red minus results?


A solution could be to add a ValueConverter that transforms the value to a brush. Bind the value to the Foreground property using the converter.

Here is an example

EDIT Another option would be to add an extra property to the object you are binding to. The property would be a Brush that changes with the number to the correct color. Then just bind the Foreground to the property. This approach is common in MVVM.

0

精彩评论

暂无评论...
验证码 换一张
取 消