Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/how-to-draw-text-to-a-control-background.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

1.9 KiB

title, ms.date, helpviewer_keywords, ms.assetid
title ms.date helpviewer_keywords ms.assetid
How to: Draw Text to a Control's Background 03/30/2017
controls [WPF], drawing text to backgrounds
text [WPF], drawing to control backgrounds
drawing [WPF], text to control backgrounds
backgrounds [WPF], drawing text to
typography [WPF], drawing text to control backgrounds
686d8fba-f61c-4974-a871-c635d67a7f69

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.

Screenshot of 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