abdolah
abdolah

Reputation: 564

dynamically set Path.Data for System.Windows.Shapes.Path

I'm writing the following code to create a chart with System.Windows.Shapes.Path. now I'm setting it's PathFigure in the xaml code:

<Grid FlowDirection="LeftToRight">
    <Canvas  HorizontalAlignment="Center" Name="PitchCanvas" VerticalAlignment="Center" Margin="30,0,370,470">
        <Canvas.RenderTransform>
            <ScaleTransform ScaleX="{Binding ElementName=SignalScaleSlider, Path=Value}" />
        </Canvas.RenderTransform>
        <Path Data="M 10,450 L 10,10 M 26,450 L 26,10 M 42,450 L 42,10 M 58,450 L 58,10 M 74,450 L 74,10 M 90,450 L 
              90,10 M 106,450 L 106,10 M 122,450 L 122,10  M 138,450 L 138,10 M 154,450 L 154,10 M 170,450 L 170,10
                  M 202,450 L 202,10 M 234,450 L 234,10 M 266,450 L 266,10 M 350,450 L 350,10
                  M 10,350 L 350,350 M 10,330 L 350,330 M 10,310 L 350,310 M 10,290 L 350,290 M 10,270 L 350,270 M 10,250 L 350,250
                  M 10,230 L 350,230 M 10,210 L 350,210 M 10,190 L 350,190 M 10,170 L 350,170 M 10,150 L 350,150 M 10,130 L 350,130 
                  M 10,110 L 350,110 M 10,90 L 350,90 M 10,70 L 350,70 M 10,50 L 350,50 M 10,30 L 350,30 M 10,10 L 350,10
                  " Height="454" Name="RangeSignalPath1" Stroke="Silver" StrokeThickness="0.5" />
        <Path Data=" M 10,450 L 350,450  M 10,330 L 350,330 M 10,230 L 350,230 M 10,130 L 350,130 M 10,30 L 350,30"
                  Name="RangeSignalPath2" Stroke="Silver" StrokeThickness="1" Height="387" />

Is there any way to write it's Path'Data dynamically (in c# code) instead of xaml code?

as you see it takes a lot of time to set the values of it but with a for loop it will be more simple.

Upvotes: 3

Views: 2100

Answers (1)

gomi42
gomi42

Reputation: 2519

Create the string as you like. Use Geometry.Parse(), it returns a Geometry object that you assign to RangeSignalPath1.Data:

Geometry g = Geometry.Parse(<your string>);
RangeSignalPath1.Data = g;

Upvotes: 7

Related Questions