我想画一条曲线 当我从xaml中绘制它时 - 一切都很好: 我可以看到曲线
<Grid x:Name="mainGrid">
<Path Stroke="#255468" StrokeThickness="12" Opacity="20" >
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure x:Name="pf" StartPoint="60,60">
<PathFigure.Segments>
<PathSegmentCollection>
<PolyBezierSegment Points="70,60 100,300 150,150 200,200 "/>
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
<Button Content="Button" HorizontalAlignment="Left" Height="54" Margin="21,0,0,24" VerticalAlignment="Bottom" Width="59" Click="Button_Click"/>
</Grid>
但是当我从后面的代码中绘制相同的行时 - 它不起作用:
<Grid x:Name="mainGrid">
<Path x:Name="orangePath" Stroke="Orange" StrokeThickness="12" Opacity="20"/>
<Button Content="Button" HorizontalAlignment="Left" Height="54" Margin="21,0,0,24" VerticalAlignment="Bottom" Width="59" Click="Button_Click"/>
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
orangePath = new Path();
PathFigure pathFigure = new PathFigure();
pathFigure.StartPoint = new Point(60, 60);
var ppp = new PolyBezierSegment();
ppp.Points.Add(new Point(70, 60));
ppp.Points.Add(new Point(100, 300));
ppp.Points.Add(new Point(150, 150));
ppp.Points.Add(new Point(200, 200));
pathFigure.Segments.Add(ppp);
PathGeometry pathGeometry = new PathGeometry();
pathGeometry.Figures = new PathFigureCollection();
pathGeometry.Figures.Add(pathFigure);
orangePath.Data = pathGeometry;
}
可能有什么问题? 我必须动态添加点。在运行时。 谢谢
答案 0 :(得分:2)
问题是你正在创建一个新路径。删除这一行:
orangePath = new Path();
它应该可以正常工作。