动态设置System.Windows.Shapes.Path的Path.Data

时间:2014-02-02 17:12:25

标签: c# wpf

我正在编写以下代码以使用System.Windows.Shapes.Path创建图表。 现在我在xaml代码中设置它的PathFigure:

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

有没有办法动态地编写它的Path'Data(在c#代码中)而不是xaml代码?

如您所见,设置其值需要花费大量时间,但使用for循环会更简单。

1 个答案:

答案 0 :(得分:5)

根据需要创建字符串。使用Geometry.Parse(),它会返回您分配给Geometry的{​​{1}}个对象:

RangeSignalPath1.Data