centering-text-on-a-wpf-shape

本文介绍如何在WPF中将文本居中显示于不同形状内,包括使用Border控件模拟圆形和椭圆,以及利用VisualBrush实现三角形和其他形状内的文本布局。

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.

http://www.pdsa.com/downloads Choose Tips & Tricks, then "WPF Text and Shapes" from the drop-down.

Good Luck With Your Coding,
Paul Sheriff

内容概要:本资源聚焦于配电网在发生故障后的两阶段鲁棒恢复研究,旨在提升电力系统在不确定性条件下的恢复能力与运行可靠性。研究采用两阶段优化方法,第一阶段进行预恢复决策,如网络重构、分布式电源出力调整等,以最小化预期损失;第二阶段则针对实际发生的故障场景实施校正控制,利用鲁棒优化理论应对负荷波动、新能源出力不确定性等因素,确保恢复方案的可行性与强健性。资源提供了完整的Matlab代码实现,复现了相关顶刊研究成果,便于使用者深入理解模型构建、算法求解及仿真分析全过程。; 适合人群:具备电力系统分析、优化理论基础及Matlab编程能力的研究生、科研人员及电力行业工程师。; 使用场景及目标:① 学习并掌握配电网故障恢复的先进优化方法,特别是两阶段鲁棒优化模型的构建与应用;② 复现和验证顶刊论文中的算法,为自身科研工作提供技术参考和代码基础;③ 将所学方法拓展应用于微电网、主动配电网等新型电力系统的可靠性评估与优化调度研究。; 阅读建议:学习者应结合提供的Matlab代码,仔细研读模型的数学公式与求解逻辑,重点关注不确定性建模、两阶段决策变量的设定以及鲁棒对等转换技巧。建议在掌握基础案例后,尝试修改参数或引入新的约束条件进行扩展研究,以深化理解并提升创新能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值