在wpf中绘制垂直线

时间:2013-04-04 13:03:55

标签: c# wpf

我编写了以下用于在画布上生成线条的代码

XAML

<Canvas HorizontalAlignment="Left" 
        x:Name="canvas1" Height="219" 
        Margin="10,10,0,0" Grid.Row="1" 
        VerticalAlignment="Top" Width="365"/>

C#

private void Draw()
{
    canvas1.Children.Clear();
    for (int i = 0; i < data.Length; i++)
    {
        data[i] = i;
        lines[i] = new Line()
        {
            X1 = leftMargin,
            Y1 = i * scale,
            X2 = i * scale,
            Y2 = i * scale,
            StrokeThickness = 2,
            Stroke = new SolidColorBrush(Colors.Black)
        };
        canvas1.Children.Add(lines[i]);
    }
}

enter image description here

但是我想画如下的线条。如何旋转画布以获得所需的输出 enter image description here

3 个答案:

答案 0 :(得分:1)

<Canvas.RenderTransform>
    <RotateTransform CenterX="110" CenterY="183" Angle="270" />
</Canvas.RenderTransform>

或代码:

Canvas.RenderTransform = new RotateTransform(270, 109.5, 182.5);

这样的东西?

答案 1 :(得分:1)

如果要旋转画布,只需在其上应用变换:

<Canvas.RenderTransform>
  <RotateTransform CenterX="110" CenterY="183" Angle="270" />
</Canvas.RenderTransform>

如果您想这样做,John Willemse建议您将代码更改为:

X1 = i * scale,
Y1 = bottomMargin,
X2 = i * scale,
Y2 = i * scale,

答案 2 :(得分:1)

x = 0且y = 0是左上角(不是左下角)所以y就像上下一样

private void Draw()
{
    Line[] lines = new Line[100];
    int scale = 3;
    canvas1.Children.Clear();
    int yStart = 290;
    for (int i = 0; i < lines.Length; i++)
    {
        //data[i] = i;
        lines[i] = new Line()
        {
            X1 = i * scale,
            Y1 = yStart,
            X2 = i * scale,
            Y2 = 300 - (i * scale),
            StrokeThickness = 1,
            Stroke = new SolidColorBrush(Colors.Black)
        };
        canvas1.Children.Add(lines[i]);
    }
}