开发者

Change TextBlock foreground color based on the background [duplicate]

This question already has answers here: 开发者_运维百科 Progress bar with dynamic text & text color update (3 answers) Closed 2 years ago.

I'm looking for a simple way to change the foreground color of a TextBlock based on the color of what is behind it, so that it is more readable. Since an image is more explicit than words, here's what I want:

Change TextBlock foreground color based on the background [duplicate]

I assume it could be done with a custom shader effect, but I have no idea how to create it... Anyway, perhaps there is a simpler solution.

Any idea would be welcome!


Assuming the above is a progressbar, here is a great solution:

WPF progress bar with dynamic text & text color update


Quick and dirty method:

Add both the white and grey textblocks, ensuring the white textblock is "on top" of the grey textblock. Bind the text of the white textblock to that of the grey textblock, so they stay the same.

Add an opacity mask to the white textblock, of which the position and/or size (or whatever required!) is bound to the position and/or size of the green rectangle (not sure if that's a templated ProgressBar or a custom control, but either way it could be done).

This would then give the effect of the text over the green bar being white.


You could write an Valueconverter (implement IValueConverter) and pass the BackgroundColor as the converter Parameter. based on the parameter you convert the forground of the Textblock to the desired Value.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜