2013-05-02 120 views
0

我在我的MainWindow中有一个Canvas,我在那里画了一条线。当它绘制我的Canvas的宽度/高度时,绘图将继续到我的MainWindow。我的代码中是否有错误,或者是正常的?C#画布将被覆盖

<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; 
} 

THX

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

回答

2

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