c# - 在wpf中绘制垂直线

标签 c# wpf

我编写了以下代码来在 Canvas 上生成线条

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

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

最佳答案

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

关于c# - 在wpf中绘制垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15812037/

相关文章:

c# - 合并多个音频和视频文件

c# - 等到委托(delegate)被调用

c# - 将 Windows 窗体值发送到 wpf 控件以更新位于 wpf 控件中的属性。

sql-server - 从 Visual Studio 2010 创建 .MDF 文件时出现问题

c# - 将 DateTime 序列化覆盖为字符串

c# - 如何从 Windows Phone 应用程序发送带有代理地址的 HttpWebRequest

C# 在项目之间共享对象

wpf - x : stand for in x:key and x:name ? 为什么是冒号,为什么不是 y :?

wpf - 如何在 WPF 中为按钮创建右键单击上下文菜单

c# - 当文本宽度大于列宽时跨越 DataGridCell