2

我有一个 WPF 项目,我必须lines在项目中绘制一些。但是当我调整窗口大小时,lines不会调整大小,因为我使用画布坐标来绘制线条。有人告诉我如何使线条调整大小随窗口大小而变化吗?

我的代码:

public static void drawGridLines(MainWindow main)
    {
            double axisX = 10;
            Line lastLine = new Line();
            lastLine.X2 = axisX;
            lastLine.Y2 = 15;
            double y = 0;
            double x = 0;
            bool first = true;
            int[] point = new int[10] { 1, 3, 8, 9, 9, 0, 7, 5, 4, 1 };

        for (int i = 0; i < point.Length; i++) // iterate over your gridview rows 
            {
                Line newline = new Line();

                newline.X1 = lastLine.X2;
                newline.Y1 = lastLine.Y2;

                newline.X2 = axisX + (Point[i] * 5); // calculate X position of the current cell 
                newline.Y2 = lastLine.Y2 + 10; // calculate Y position of the current cell 
                x = newline.X2;
                y = newline.Y2;

                if (!first)
                {
                    // first minimum cell should't be drawn, it is just the start point for next line                  
                    drawLine(main, newline);
                }
                else
                {
                    first = false;
                }

                lastLine = newline;
            }


public static void drawLine(MainWindow main, Line line)
    {
        line.HorizontalAlignment = HorizontalAlignment.Left;
        line.VerticalAlignment = VerticalAlignment.Center;
        line.Stroke = System.Windows.Media.Brushes.SteelBlue;
        line.StrokeThickness = 1.5;
        main.myLineCanvas.Children.Add(line);

    }
4

3 回答 3

2

把你Canvas放在一个Viewbox

<Viewbox>
    <Canvas x:Name="myLineCanvas" />
</Viewbox>

Stretch您可以使用和更改其行为StretchDirection

在此处输入图像描述

于 2012-11-16T14:58:32.760 回答
1

您应该尝试使用 ViewBox,看看它是否适合您的情况,就像用 ViewBox 围绕画布一样简单。

<Window ...
    ...>

    <ViewBox>
        <Canvas .....>
        </Canvas>
    </ViewBox>
</Window>
于 2012-11-16T14:57:46.983 回答
0

Window 有一个 SizeChanged 事件。在您的处理程序中,您可以获取窗口的新大小并相应地设置行的端点。

于 2012-11-16T14:51:33.853 回答