简体   繁体   English

如何在C#中设置FormattedText背景颜色

[英]How to set FormattedText background color in C#

Is there a way to set background color for FormattedText in C#? 有没有办法在C#中为FormattedText设置背景颜色? 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. 与绘制到DrawingContext任何内容一样,对象的属性仅控制对象本身,即实际为该对象绘制的内容,而不是它后面或周围的内容。 Drawing a rectangle behind the text is the most obvious work-around, and would be entirely appropriate when using the object in a DrawingContext . 在文本后面绘制一个矩形是最明显的解决方法,并且在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. 您必须决定在您自己的特定场景中最合适的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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