在WPF中的形状上绘制文本 [英] Draw text on a shape in a wpf

查看:240
本文介绍了在WPF中的形状上绘制文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些人可能觉得这个问题很乏味,但我仍然不太习惯使用wpf绘图。我想在画布上四处移动的Rectangle上添加格式化的文本,并且得到了覆盖UIElement.OnRender方法的提示。但是,我不知道是否应该覆盖canvas类或Shape类。在任何正确的情况下,如示例中所述,指的是该方法的drawingContext参数: http://msdn.microsoft.com/zh-cn/library/bb613560.aspx#FormattedText_Object 吗?



文本是最终分配给形状的,还是视觉临时效果,无法随画布上的形状一起移动?



还有其他在图形上绘制文本的有效方法吗?

解决方案

您可以通过将两个控件放在允许控件重叠的父容器中来在矩形的顶部绘制文本,例如 Grid 画布

 < Grid> 
< Rectangle Fill = Red Stroke = Black
Horizo​​ntalAlignement = Stretch VerticalAlignment = Stretch />

<标签内容=测试
Horizo​​ntalAlignment = Center VerticalAlignment = Center />
< / Grid>

然后可以将所需的任何格式应用于 Label 矩形,您可以通过设置 Grid

Some of you maybe find this question dull but I am still not deeply accustomed to wpf drawing. I want to add formatted text on a Rectangle which moves around on a canvas and I have got a hint to override the UIElement.OnRender method. However I do not know if I should override the canvas class or the Shape class. In any correct case, to what refers the drawingContext parameter of the method as described in the example: http://msdn.microsoft.com/en-us/library/bb613560.aspx#FormattedText_Object ?

Is the text ultimately assigned to the shape or is it a visual temporary effect that cannot move along with the shape on the canvas?

Is there any further effective means of drawing text on a shape?

解决方案

You can draw Text on top of a Rectangle by placing both controls in a parent container that allows controls to overlap, such as a Grid or a Canvas

<Grid>
    <Rectangle Fill="Red" Stroke="Black" 
               HorizontalAlignement="Stretch" VerticalAlignment="Stretch" />

    <Label Content="Test"
           HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>

You can then apply whatever formatting you want to the Label, the Rectangle, and you can move the group around by setting the positioning of the Grid

这篇关于在WPF中的形状上绘制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆