更改控件在画布中的零点 [英] Change controls zero point in canvas

查看:90
本文介绍了更改控件在画布中的零点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在画布上绘制项目时,位置从画布内项目的顶部开始:

When i draw items in canvas, the position is take from top of the item inside canvas:

<Canvas Name="cnvMain"
        Width="80"
        Height="80">
  <Ellipse Canvas.Top="20"
           Canvas.Left="40"
           Width="40"
           Height="40"
           Fill="Gray" />
</Canvas>

现在我需要从元素的底部进行测量,如下所示:

Now what i would need is to take the measurement from the bottom of the element, like this:

最后,我希望能够设置从椭圆底部到画布顶部的距离.注意:椭圆"有时不能是一个圆形,并且不能将其倒置.另外我也不知道运行时之前的高度,因此在xaml中设置负边距也将不起作用.

(图片和示例xaml摘自:

(Picture and example xaml taken from: http://weblogs.asp.net/psheriff/centering-text-within-a-wpf-shape-using-a-canvas)

推荐答案

如果要指定椭圆底部到画布底部的距离,可以替换 Canvas.Top 通过 Canvas.Bottom :

If you want to specify the distance of the bottom of the Ellipse to the bottom of the Canvas, you could replace Canvas.Top by Canvas.Bottom:

<Ellipse Canvas.Bottom="20" Canvas.Left="40" Width="40" Height="40" Fill="Gray"/>

对于从椭圆底部到画布顶部的距离,您可以指定负的Margin:

For the distance from the bottom of the Ellipse to the top of the Canvas, you may specify a negative Margin:

<Ellipse Canvas.Top="20" Canvas.Left="40" Width="40" Height="40" Fill="Gray"
         Margin="0,-40,0,0"/>

或适当的RenderTransform:

or an appropriate RenderTransform:

<Ellipse Canvas.Top="20" Canvas.Left="40" Width="40" Height="40" Fill="Gray">
    <Ellipse.RenderTransform>
        <ScaleTransform ScaleY="-1"/>
    </Ellipse.RenderTransform>
</Ellipse>

为了反转整个Canvas坐标系的y方向,您可以将RenderTransform应用于Canvas:

In order to invert the y direction of the whole Canvas coordinate system, you may apply a RenderTransform to the Canvas:

<Canvas ... RenderTransformOrigin="0,0.5">
    <Canvas.RenderTransform>
        <ScaleTransform ScaleY="-1"/>
    </Canvas.RenderTransform>
    ...
</Canvas>

您还可以将椭圆放置在另一个高度为零的画布中,并将其附加到较低的画布边框:

You may also put the Ellipse in another Canvas with zero Height, and attach it to the lower Canvas border:

<Canvas Canvas.Top="20" Canvas.Left="40" Width="0" Height="0">
    <Ellipse Canvas.Bottom="0" Width="40" Height="40" Fill="Gray"/>
</Canvas>

这篇关于更改控件在画布中的零点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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