wpf 在wpf中绘制垂直线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15812037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:27:56  来源:igfitidea点击:

Draw vertical lines in wpf

c#wpf

提问by santosh singh

I have written following code for generating lines on canvas

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

XAML

XAML

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

C#

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

在此处输入图片说明

But I want to draw lines as below.How I can rotatle the canvas to achieve the desired output enter image description here

但我想画线如下。如何旋转画布以实现所需的输出 在此处输入图片说明

采纳答案by paparazzo

x = 0 and y = 0 is upper left corner (not lower left) so y is like up side down

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]);
    }
}

回答by mlemay

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

or by code:

或通过代码:

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

Something like this?

像这样的东西?

回答by Luke Marlin

If you want to rotate the canvas, you can simply apply a transform on it:

如果要旋转画布,只需对其应用变换即可:

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

If you want to do as John Willemse suggested change your code to this :

如果您想按照 John Willemse 的建议将代码更改为:

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