Permalink
Fetching contributors…
Cannot retrieve contributors at this time
40 lines (34 sloc) 2.33 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Draw Text to a Control's Background
03/30/2017
.net-framework
dotnet-wpf
article
controls, drawing text to backgrounds
text, drawing to control backgrounds
drawing, text to control backgrounds
backgrounds, drawing text to
typography, drawing text to control backgrounds
686d8fba-f61c-4974-a871-c635d67a7f69
10
dotnet-bot
dotnetcontent
wpickett

How to: Draw Text to a Control's Background

You can draw text directly to the background of a control by converting a text string to a xref:System.Windows.Media.FormattedText object, and then drawing the object to the control's xref:System.Windows.Media.DrawingContext. You can also use this technique for drawing to the background of objects derived from xref:System.Windows.Controls.Panel, such as xref:System.Windows.Controls.Canvas and xref:System.Windows.Controls.StackPanel.

Controls displaying text as background
Example of controls with custom text backgrounds

Example

To draw to the background of a control, create a new xref:System.Windows.Media.DrawingBrush object and draw the converted text to the object's xref:System.Windows.Media.DrawingContext. Then, assign the new xref:System.Windows.Media.DrawingBrush to the control's background property.

The following code example shows how to create a xref:System.Windows.Media.FormattedText object and draw to the background of a xref:System.Windows.Controls.Label and xref:System.Windows.Controls.Button object.

[!code-csharpDrawTextToControlBackground#DrawTextToControlBackground1]

See Also

xref:System.Windows.Media.FormattedText
Drawing Formatted Text