博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
centering-text-on-a-wpf-shape
阅读量:4031 次
发布时间:2019-05-24

本文共 4117 字,大约阅读时间需要 13 分钟。

http://thatstoday.com/article/1160750/centering-text-on-a-wpf-shape

 

 

 

In a WPF application I am building right now, I had a need to create different sets of shapes and put some text within those shapesThe various shapes I needed were things like rectangles, circles, ellipses and triangles.

WPF can create these shapes; however, shapes in WPF are not containers, so you cannot add any text inside of themThis article will show you how to put text into each of these shapes in a couple of different ways.

Using a Border Control

If you wish to put text into an Ellipse, you can simulate this using a Border control and play with the Width, Padding and CornerRadius properties of this control.

Below is some sample code that draws text like that shown in Figure 1.

<Border CornerRadius="50"

        Width="60"
        Margin="10"
        Padding="4"
        Background="Aquamarine"
        BorderBrush="Black"
        BorderThickness="1">
   <TextBlock HorizontalAlignment="Center">Test</TextBlock>
</Border>

Figure 1  

Figure 1: Using a Border control to simulate an ellipse.

You can set the width and height of the Border control to create a circle as well.

Below is some XAML that creates a circle with text in the middle as shown in Figure 2.

<Border CornerRadius="50"

        Width="60"
        Height="60"
        Margin="10"
        Padding="0,20,0,0"
        Background="Aquamarine"
        BorderBrush="Black"
        BorderThickness="1">
  <TextBlock HorizontalAlignment="Center">Test
  </TextBlock>
</Border>

 

Figure 2: Using a Border control to simulate a circle.

Using a VisualBrush

If you wish to use a Triangle and put some text within that triangle, you will need to use a Polygon control to create that triangle.

For example, here is code to create a triangle without any words in it.

<Polygon Points="25,0 5,30 45,30"

         Fill="LightBlue"
         Stroke="Black"
         StrokeThickness="2" />

To add words, like that shown in Figure 3, you need to use this Polygon control as the background of a TextBlock control.

The XAML shown below is what is used to create Figure 3.

<TextBlock Text="Test"

           Width="50"
           Height="50"
           Padding="13,28,0,0">
   <TextBlock.Background>
     <VisualBrush>
       <VisualBrush.Visual>
          <Polygon Points="25,0 5,30 45,30"
                   Fill="LightBlue"
                   Stroke="Black"
                   StrokeThickness="2" />
       </VisualBrush.Visual>
     </VisualBrush>
   </TextBlock.Background>
</TextBlock>

There are two keys to this XAML.

First, you use the VisualBrush object of the Background of the TextBlock object to give you a place to put the Polygon controlSecondly, 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.
Take the above XAML and put it in a new window and then adjust the height, width and text valuesYou will see that you need to adjust the Padding property to make the text fall into the correct location.
 

Figure 3: Using a Visual Brush.

Here is another example of a Visual Brush, this time using an Ellipse control.

The results of this XAML can be seen in Figure 4.

<TextBlock Text="Test"

           Height="40"
           Width="40"
           Padding="8,10,0,0">
  <TextBlock.Background>
    <VisualBrush>
      <VisualBrush.Visual>
         <Ellipse Height="20"
                  Width="20"
                  Fill="LightBlue" />
      </VisualBrush.Visual>
    </VisualBrush>
  </TextBlock.Background>
</TextBlock>

Notice the Padding is different from the Padding used when using the Polygon control.

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.

 

Figure 4: Using an Ellipse as the background for a TextBlock

Summary

This article presented a couple of different methods of placing text in the middle of shape objects in WPF.

You do have to do some tweaking of properties to get the text to appear in the middle of the shape, but for many uses, these techniques work wellIn my next blog post, I will show you how to use a Canvas, a Shape, a TextBlock and a multi-binding value converter to center the text automatically.

 

NOTE: You can download the complete sample code at my website.

Choose Tips & Tricks, then "WPF Text and Shapes" from the drop-down.

Good Luck With Your Coding,

Paul Sheriff

转载地址:http://raqbi.baihongyu.com/

你可能感兴趣的文章
Python自动化之pytest常用插件
查看>>
Python自动化之pytest框架使用详解
查看>>
【正则表达式】以个人的理解帮助大家认识正则表达式
查看>>
性能调优之iostat命令详解
查看>>
性能调优之iftop命令详解
查看>>
非关系型数据库(nosql)介绍
查看>>
移动端自动化测试-Windows-Android-Appium环境搭建
查看>>
Xpath使用方法
查看>>
移动端自动化测试-Mac-IOS-Appium环境搭建
查看>>
Selenium之前世今生
查看>>
Selenium-WebDriverApi接口详解
查看>>
Selenium-ActionChains Api接口详解
查看>>
Selenium-Switch与SelectApi接口详解
查看>>
Selenium-Css Selector使用方法
查看>>
Linux常用统计命令之wc
查看>>
测试必会之 Linux 三剑客之 sed
查看>>
Socket请求XML客户端程序
查看>>
Java中数字转大写货币(支持到千亿)
查看>>
Java.nio
查看>>
函数模版类模版和偏特化泛化的总结
查看>>