[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Programmatically center text at runtime with WPF and C#

[Programmatically center text at runtime with WPF and C#]

This program uses the following code at runtime to draw a circle with some text centered in it.

private void Window_Loaded(object sender, RoutedEventArgs e) { double cx = 50; double cy = 50; double radius = 20; Rect rect = new Rect( cx - radius, cy - radius, 2 * radius, 2 * radius); Ellipse ellipse = new Ellipse(); ellipse.Stroke = Brushes.Blue; ellipse.Fill = Brushes.LightBlue; Canvas.SetLeft(ellipse, cx - radius); Canvas.SetTop(ellipse, cy - radius); ellipse.Width = 2 * radius; ellipse.Height = 2 * radius; canDraw.Children.Add(ellipse); Label label = new Label(); label.Content = "Hello"; label.HorizontalContentAlignment = HorizontalAlignment.Center; label.VerticalContentAlignment = VerticalAlignment.Center; Canvas.SetLeft(label, cx - radius); Canvas.SetTop(label, cy - radius); label.Width = 2 * radius; label.Height = 2 * radius; canDraw.Children.Add(label); }

To draw in WPF, you normally create objects to represent whatever it is that you want to draw. This code first creates an Ellipse object. You might think the class's constructor would let you set obvious properties like colors, size, and position, but no, that would be too easy and contradict WPF's unofficial slogan, "Twice as flexible and only five times as hard!"

After creating the ellipse, the code sets its stroke and fill colors. Note that those properties are actually brushes not simple colors.

Next the code sets the ellipse's X and Y positions. Microsoft didn't give the Ellipse class those properties, however. Instead they are provided by the Canvas control that will contain the ellipse.

The program sets the ellipse's width and height properties and then adds it to the canvas control's Children collection.

Next the code creates a Label object and sets its Content property. It uses the HorizontalAlignment and VerticalAlignment properties to center the label's text within its area. The program then sets the label's position and size and adds it to the Canvas control's Children collection much as it did with the ellipse.

The final thing to notice is that the program draws the objects in the order in which they are storeed in their containers' Children collections. In this example, that means it draws the ellipse before it draws the label. That's important because, if it drew the label first, the ellipse would cover it and you wouldn't see it.

This also means that you can change the drawing order by rearranging items in their Children collections.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.