在 WPF 网格中绘制对角线 [英] Draw Diagonal Line in WPF Grid

查看:16
本文介绍了在 WPF 网格中绘制对角线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我正在尝试在 WPF 中做一些相对简单的事情,但我一生都无法弄清楚如何做;并且认为我可能正处于过度复杂化的边缘.

I think I am trying to do something relatively simple in WPF, but can't for the life of me figure out how; and think I am probably on the verge of overcomplicating it.

如果我有一个 3 行 3 列的网格,并且我想连接两个单元格的角以创建对角线边框,那么最好的方法是什么?

If I had a grid which was 3 rows and 3 columns, and I wanted to join the corners of two cells to create a diagonal border, what would be the best way of doing so?

如果控件被调整大小(如此绑定到单元格的角落?),线条应该重新调整大小.

The lines should ideally re-size if the control is resized (so bound to the corners of the cell?).

基本上我想在这里托管的图表中创建红线:Example Pic http://imm.io/7A4L

Essentially I would like to create the red lines in the diagram hosted here: Example Pic http://imm.io/7A4L

推荐答案

您可以使用 Stretch=Fill 的路径.对于示例中的右上角单元格,您将使用:

You could use a Path with Stretch=Fill. For the top right cell in your example, you would use:

<Path Grid.Row="2" Grid.Column="0" Stroke="Red" StrokeThickness="2" Stretch="Fill">
    <Path.Data>
        <LineGeometry StartPoint="0,0" EndPoint="1,1" />
    </Path.Data>
</Path>

填充"拉伸使 Path 拉伸以填充其父级,这给人的印象是 LineGeometry 的坐标是相对的(X=0,Y=0 是左上角,X=1,Y=1 是底部对).

The "Fill" stretch makes the Path stretch to fill its parent, which gives the impression that the coordinates of the LineGeometry are relative (X=0,Y=0 is top left, X=1,Y=1 is bottom right).

这篇关于在 WPF 网格中绘制对角线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆