This blog post shows one way to render text in the adorner layer. I created an adorner class named TextBlockAdorner, which allows you to render a TextBlock over an element by placing it into the adorner layer. If you don’t know what the adorner layer is yet, you might want to read this article.
Christian Graus recently posted a question to the WPF Forum asking how to render text in a Viewbox, but without having the text size be affected by the scaling of the Viewbox. The solution I came up with is shown below.
Suppose that this is the Viewbox which we want to place text over, and have that text not be scaled or modified:
In the code-behind for the Window which contains the Viewbox, we can use my TextBlockAdorner to specify what text to display over the Viewbox.
After we’re done with that, the UI looks like this:
As you can see above, the text in the ViewBox is scaled down to a smaller size than the “natural” font size in the Window. The adorner text uses the “natural” font size because it is not affected by the Viewbox.
The demo app does not provide a clean way of repositioning the adorner text, so that is an exercise left up to the reader. There are numerous examples on the Web which show how to use a TranslateTransform to move the contents of an adorner, such as this article and my ListViewDragDropManager’s DragAdorner class.
Download the demo project here: TextBlockAdorner Demo (demo project) Be sure to change the file extension from .DOC to .ZIP and then decompress it.