简体   繁体   中英

How to set FormattedText background color in C#

Is there a way to set background color for FormattedText in C#? I'm using a it something like this:

var formattedText = new FormattedText(...);
Point point = new Point(x,y);
// Draw the text onto a drawing context at specific point position
dc.DrawText(formattedText, point);

I'm trying to change the background color of the formatted text. Otherwise, I have to draw a rectangle behind the text.

No, you cannot. As is the case for anything you draw into a DrawingContext , the properties of the object control only the object itself, ie what's actually drawn for that object, not what's behind it or around it. Drawing a rectangle behind the text is the most obvious work-around, and would be entirely appropriate when using the object in a DrawingContext .

You could set the background of some element in which the text is eventually hosted, but of course that would set the whole background, not necessarily just what's behind the text (depending on what else is getting drawing into the context).

You'll have to decide what's most appropriate in your own specific scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM