我有一个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);
}
答案 0 :(得分:2)
将Canvas
放入Viewbox
:
<Viewbox>
<Canvas x:Name="myLineCanvas" />
</Viewbox>
您可以使用Stretch
和StretchDirection
更改其行为。
答案 1 :(得分:1)
您应该尝试使用ViewBox并查看它是否适用于您的情况,就像使用ViewBox围绕您的画布一样简单。
<Window ...
...>
<ViewBox>
<Canvas .....>
</Canvas>
</ViewBox>
</Window>
答案 2 :(得分:0)
Window有一个SizeChanged事件。在您的处理程序中,您可以获取窗口的新大小并相应地设置行的端点。