0

我有一个Canvas,我在MainWindow那里画一条线。当它在 my 的宽度/高度上Canvas绘制时,在 my 中继续绘制MainWindow。我的代码有错误还是正常?

<Canvas x:Name="coordinateSystem" HorizontalAlignment="Right" Height="580" Margin="0,10,283,0" VerticalAlignment="Top" Width="1024" Cursor="Cross" UseLayoutRounding="False"/>

这是我每次为我的线获得新坐标时调用的函数:

// xOld, yOld and t are static
// t represents the time
private void drawPoly(double value) 
{    
    t++;
    Point pOne = new Point(xOld, yOld);
    Point pTwo = new Point(t, value);

    GeometryGroup lineGroup = new GeometryGroup();
    LineGeometry connectorGeometry = new LineGeometry();
    connectorGeometry.StartPoint = pOne;
    connectorGeometry.EndPoint = pTwo;
    lineGroup.Children.Add(connectorGeometry);
    System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
    path.Data = lineGroup;
    path.StrokeThickness = 1;
    path.Stroke = path.Fill = Brushes.Red;

    coordinateSystem.Children.Add(path);    

    xOld = t;
    yOld = value;
}

谢谢

PS:有没有办法保存所有绘制的点?我想稍后调整我的画布大小(缩小/放大),或者如果时间要在画布上大幅移动我的画线,然后我需要再次绘制所有点。

4

1 回答 1

2

画布不会剪切子元素。如果要阻止子元素在 Canvas 之外绘制,则需要将 ClipToBounds 设置为 true 或设置 Canvas 的 Clip。

于 2013-05-02T17:09:46.460 回答