Matvey
Matvey

Reputation: 163

Wpf path does not render

I wand to draw a curve When I draw it from xaml - everything is good: I can see the curve

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

But when I draw the same line from code behind - it does not work :

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

    }

What could be a problem ? I have to add the points dynamically. At the runtime. Thank you

Upvotes: 0

Views: 551

Answers (1)

Andrei Tătar
Andrei Tătar

Reputation: 8295

The problem is you are creating a new Path. Remove this line:

orangePath = new Path();

And it should work just fine.

Upvotes: 2

Related Questions