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

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

在形状中放入一个TextBox,类似可以放入RichTextBox

Centering Text on a WPF Shape

http://weblogs.asp.net/psheriff/archive/2009/12/15/centering-text-on-a-wpf-shape.aspx

 

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 shapes. The 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 them. This 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 control. Secondly, 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 values. You 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 well. In 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

** SPECIAL OFFER FOR MY BLOG READERS **

Visit for a free eBook on "Fundamentals of N-Tier".

Posted: by | with Filed under: ,

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

你可能感兴趣的文章
mac:移动python包路径
查看>>
mysql:sql create database新建utf8mb4 数据库
查看>>
mysql:sql alter database修改数据库字符集
查看>>
mysql:sql alter table 修改列属性的字符集
查看>>
mysql:sql drop table (删除表)
查看>>
mysql:sql truncate (清除表数据)
查看>>
scrapy:xpath string(.)非常注意问题
查看>>
yuv to rgb 转换失败呀。天呀。谁来帮帮我呀。
查看>>
yuv420 format
查看>>
单纯的把Y通道提取出来能正确显示出灰度图来为什么我的Qt就显示不出来呢转换有问题呀?
查看>>
YUV420只绘制Y通道
查看>>
yuv420 还原为RGB图像
查看>>
LED恒流驱动芯片
查看>>
驱动TFT要SDRAM做为显示缓存
查看>>
使用file查看可执行文件的平台性,x86 or arm ?
查看>>
qt5 everywhere 编译summary
查看>>
qt5 everywhere编译完成后,找不到qmake
查看>>
arm-linux开机读取硬件时钟,设置系统时钟。
查看>>
交叉编译在x86上调试好的qt程序
查看>>
/dev/input/event0 键盘输入
查看>>