You use a WPF Canvas control to position child controls at a specified point on the Canvas. Before you can learn that technique, you first need to learn to use a Canvas, a Shape, a TextBlock and MultiBinding converters.
The ultimate goal of this article is to show you how to move all of this code into one user control that you can use over and over again. Using a Visual Brush within a TextBlock control works, but it is not very re-usable code. You will need to make minor adjustments based on the shape that you are using and the height and width of the control, and the text you are putting into the TextBlock. Notice the Padding attribute is different than when using the Polygon control. Figure 4: Use a Visual Brush to put text in an ellipse. Figure 4 shows the results of the XAML in Listing 2. Listing 2 shows another example of a Visual Brush, this time using an Ellipse control. You will see that you need to adjust the Padding property to make the text fall into the correct location within the Polygon. Take the above XAML and put it in a new window and then adjust the height, width and text values. Second, depending on the text that you fill into the TextBlock control, and the width and height of the TextBlock control, you will need to play with the Padding property to align the text to the correct location. First, you use the VisualBrush object of the Background of the TextBlock object to give you a place to put the Polygon control.
Figure 3: Use a Visual Brush to put text in a triangle. The XAML shown in Listing 1 is used to create Figure 3. To add text like that shown in Figure 3, you need to use the Polygon control as the background of a TextBlock control. The XAML below creates a triangle however, there is no text in it because a Polygon is not a container.
If you remember your high school math, a Polygon is a series of connected lines that have the same start and end point. To create a triangle shape in WPF you use a Polygon control. Figure 2: Use a Border control to simulate a circle. The XAML in the next snippet creates a circle with text in the middle, as shown in Figure 2. You can set the width and height of the Border control to create a circle as well. Figure 1: Use a Border control to simulate an ellipse. I used the code snippet below to draw text within an ellipse as shown in Figure 1. If you wish to put text into an ellipse or a circle, you can simulate this using a Border control and play with the Width, Padding and CornerRadius properties of the Border control. In this article, I will show you a few different methods of centering text in each of these shapes. The problem is WPF shapes are not containers so you cannot add any text inside of them, but WPF is flexible enough that there are often several ways to work around this. The shapes I need are rectangles, circles, ellipses and triangles. In a WPF application I am building right now, I have a need to create different shapes and center text within those shapes. However, the shapes in WPF do not allow you to add text (or any content) within the shape, as shapes are not containers. One of the ways you can make your application look great is to add some shapes like circles, triangles and rectangles. This program must be run from an elevated command prompt.") Ĭonsole.WriteLine("packageManager.WPF excels at creating great looking applications. If (args.Length packages = (IEnumerable)packageManager.FindPackages(inputPackageName, inputPublisherName) ĭisplayPackageUsers(packageManager, package) Ĭonsole.WriteLine("No packages were found.") Ĭonsole.WriteLine("packageManager.FindPackages() failed because access was denied. All rights reserved.") Ĭonsole.WriteLine("FindPackagesByNameAndPublisher sample") Console.WriteLine("Copyright (c) Microsoft Corporation.