3

我有一个程序,我可以System.Windows.Shapes.EllipseCanvas面板中拖动、旋转和调整 a 的大小。

要在画布内调整椭圆的大小并将其拖动并始终保持居中,我需要在每次其原点时进行更正,因为椭圆的原点位于左上角。

有没有办法让原点Ellipse默认在中心?

拖:

Canvas.SetTop(ellipse, newX - (ellipse.Height / 2));
Canvas.SetLeft(ellipse, newY - (ellipse.Width / 2));

调整大小:

ellipse.Height = newHeight;
ellipse.Width = newWidth;

旋转:

ellipse.RenderTransform = new RotateTransform(angle,(ellipse.Width/2),(ellipse.Height/2));
4

2 回答 2

5

如果宽度和高度是固定的,最简单的解决方案是将椭圆设置RenderTransform为一个TranslateTransformwithXY设置为负偏移量,分别等于椭圆的宽度和高度的一半:

<Ellipse Width="100" Height="100" Fill="Red">
  <Ellipse.RenderTransform>
    <TranslateTransform X="-50" Y="-50" />
  </Ellipse.RenderTransform>
</Ellipse>

请注意,使用的一个警告RenderTransform是转换不适用于布局(并且您不能将 aTranslateTransform用于LayoutTransform)。这不应该是 a 的问题,Canvas因为它处理布局的方式,尽管它可能与其他面板有问题。

于 2013-10-29T18:20:31.977 回答
0

您可以使用 Margin 属性。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="600" Width="600">
    <Grid>
        <Canvas Width="300" Height="300">
            <Ellipse x:Name="ellipse" 
                     Canvas.Left="150" Canvas.Top="150" 
                     Width="50" Height="50" 
                     Margin="-25,-25" 
                     Stroke="Red"/>
        </Canvas>
    </Grid>
</Window>
于 2014-10-17T18:32:39.367 回答