在WPF网格中绘制对角线

发布于 2024-11-26 21:04:08 字数 294 浏览 0 评论 0原文

我想我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2024-12-03 21:04:08

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

<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>

“填充”拉伸使路径拉伸以填充其父级,这给人的印象是 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:

<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>

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).

淡淡の花香 2024-12-03 21:04:08

我创建了一个示例来从后面的代码中绘制线条,这将为您提供更多控制......
我创建了一个网格,其中每个单元格中都包含画布,并且在画布加载上我正在创建一条路径并将其添加到同一个画布中...

正如@ Mathieu Garstecki 的回答,我们可以在 xaml 中实现此创建路径...如果想添加一些逻辑在创建路径之前,您可以使用我的答案
XAML

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Canvas Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="1" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="2" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="0" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="1" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="2" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>

    </Grid>

代码隐藏

private void Canvas_Loaded(object sender, RoutedEventArgs e)
        {
            var g = new StreamGeometry();
            var context = g.Open();
            context.BeginFigure(new Point(0, 0), true, true);
            context.LineTo(new Point((sender as Canvas).ActualHeight, (sender as Canvas).ActualWidth), true, true);
            context.Close();
            System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
            path.Data = g;
            path.Stroke = new SolidColorBrush(Colors.Red);
            path.StrokeThickness = 1.4;
            (sender as Canvas).Children.Add(path);
        }

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

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Canvas Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="1" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="2" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="0" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="1" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
        <Canvas Grid.Row="2" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>

    </Grid>

Code Behind

private void Canvas_Loaded(object sender, RoutedEventArgs e)
        {
            var g = new StreamGeometry();
            var context = g.Open();
            context.BeginFigure(new Point(0, 0), true, true);
            context.LineTo(new Point((sender as Canvas).ActualHeight, (sender as Canvas).ActualWidth), true, true);
            context.Close();
            System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
            path.Data = g;
            path.Stroke = new SolidColorBrush(Colors.Red);
            path.StrokeThickness = 1.4;
            (sender as Canvas).Children.Add(path);
        }

OutPut

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文