在WPF网格中绘制对角线
我想我正在尝试在 WPF 中做一些相对简单的事情,但我无法弄清楚如何做;我认为我可能正处于将其过于复杂化的边缘。
如果我有一个 3 行 3 列的网格,并且我想连接两个单元格的角以创建对角边框,那么最好的方法是什么?
理想情况下,如果调整控件的大小,则线条应该重新调整大小(因此绑定到单元格的角上?)。
本质上,我想在此处托管的图表中创建红线: 示例图片 http://imm.io/7A4L
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.
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?).
Essentially I would like to create the red lines in the diagram hosted here: Example Pic http://imm.io/7A4L
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用带有 Stretch=Fill 的路径。对于示例中的右上角单元格,您可以使用:
“填充”拉伸使路径拉伸以填充其父级,这给人的印象是 LineGeometry 的坐标是相对的(X = 0,Y = 0 是左上角,X=1,Y=1 为右下角)。
You could use a Path with Stretch=Fill. For the top right cell in your example, you would use:
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).
我创建了一个示例来从后面的代码中绘制线条,这将为您提供更多控制......
我创建了一个网格,其中每个单元格中都包含画布,并且在画布加载上我正在创建一条路径并将其添加到同一个画布中...
正如@ Mathieu Garstecki 的回答,我们可以在 xaml 中实现此创建路径...如果想添加一些逻辑在创建路径之前,您可以使用我的答案
XAML
代码隐藏
OutPut
I have created a sample to draw line from code behind which will give you more control...
I crated a grid which contains canvas in each cell and on canvas load i am creating a path and adding it to same canvas...
As @ Mathieu Garstecki answer we can achieve this crating path in xaml... if want to add some logic before creating path you can use my answer
XAML
Code Behind
OutPut