Draw text to the background of a control by accessing the controls DrawingContext
Sample Text
//File:Window.xaml.vb
Imports System
Imports System.Globalization
Imports System.Windows
Imports System.Windows.Media
Namespace WpfApplication1
Public Partial Class Window1
Inherits Window
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Private Sub WindowLoaded(sender As Object, e As EventArgs)
myLabel.Background = New DrawingBrush(DrawMyText("My Custom Label"))
myButton.Background = New DrawingBrush(DrawMyText("Display Text"))
End Sub
Private Function DrawMyText(textString As String) As Drawing
Dim drawingGroup As New DrawingGroup()
Using drawingContext As DrawingContext = drawingGroup.Open()
Dim formattedText As New FormattedText(textString, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, New Typeface("Comic Sans MS Bold"), 48, Brushes.Black)
Dim textGeometry As Geometry = formattedText.BuildGeometry(New Point(20, 0))
drawingContext.DrawRoundedRectangle(Brushes.PapayaWhip, Nothing, New Rect(New Size(formattedText.Width + 50, formattedText.Height + 5)), 5.0, 5.0)
drawingContext.DrawGeometry(Brushes.Gold, New Pen(Brushes.Maroon, 1.5), textGeometry)
Return drawingGroup
End Using
End Function
Public Sub OnButtonClick(sender As Object, e As EventArgs)
myCanvas.Background = New DrawingBrush(DrawMyText(myTextBox.Text))
End Sub
End Class
End Namespace