WPF/Path - 由多条不同线宽的线组成的几何图形?

发布于 2024-10-10 00:01:38 字数 1312 浏览 0 评论 0原文

我需要在同一张图中绘制不同颜色、粗细等的线条。

我可以创建两个 PathGeometry 实例,但无法在它们上设置颜色。

我可以创建 Path 的两个实例,但无法将它们显示在我的控件中。

我做错了什么?

感谢您的任何评论!

安德斯,丹麦。

下面的代码仅在我的控制中显示“集合”,但我认为这可能是答案的起点......

        var pathFigure1 = new PathFigure(new Point(0, 0),
                                         new List<PathSegment> {new LineSegment(new Point(10, 10), true)}, false);
        var pathFigure2 = new PathFigure(new Point(20, 20),
                                         new List<PathSegment> {new LineSegment(new Point(30, 30), true)}, false);


        var g1 = new PathGeometry(new List<PathFigure> {pathFigure1});
        var g2 = new PathGeometry(new List<PathFigure> {pathFigure2});

        var p1 = new System.Windows.Shapes.Path
                     {
                         Data = g1,
                         Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0))
                     };
        var p2 = new System.Windows.Shapes.Path
                     {
                         Data = g2,
                         Stroke = new SolidColorBrush(Color.FromRgb(170, 87, 170))
                     };

        var content = new Canvas();
        content.Children.Add(p1);
        content.Children.Add(p2);

        Content = content;

I have the need for drawing in the same drawing with lines of different color, thickness etc.

I can create two instances of PathGeometry, but I can't set color on them.

I can create two instances of Path, but can't get them displayed in my control.

What am I doing wrong?

Thanks for any comments!

Anders, Denmark.

Code below only displays "collection" in my control, but I thought it could be a starting point for answers...

        var pathFigure1 = new PathFigure(new Point(0, 0),
                                         new List<PathSegment> {new LineSegment(new Point(10, 10), true)}, false);
        var pathFigure2 = new PathFigure(new Point(20, 20),
                                         new List<PathSegment> {new LineSegment(new Point(30, 30), true)}, false);


        var g1 = new PathGeometry(new List<PathFigure> {pathFigure1});
        var g2 = new PathGeometry(new List<PathFigure> {pathFigure2});

        var p1 = new System.Windows.Shapes.Path
                     {
                         Data = g1,
                         Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0))
                     };
        var p2 = new System.Windows.Shapes.Path
                     {
                         Data = g2,
                         Stroke = new SolidColorBrush(Color.FromRgb(170, 87, 170))
                     };

        var content = new Canvas();
        content.Children.Add(p1);
        content.Children.Add(p2);

        Content = content;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

み青杉依旧 2024-10-17 00:01:38

您已经开始采用正确的方法,几何图形定义了“形状”,因此不必担心无法设置其颜色。路径将几何图形转换为屏幕上的视觉表示,因此您可以在此处设置颜色和描边粗细。

您的问题是在最后一步,您正在设置控件的内容属性。通常,此属性用于将某些数据对象与控件关联,然后您提供一个模板作为其可视化表示。

您需要做的是将路径添加为面板的子级。

例如,将 Canvas 或 Grid 添加到控件中。然后通过其 Children 集合属性将两条路径添加到 Grid / Canvas。

You have started on the right approach, a geometry defines a 'shape', so don;t worry that you cannot set its colour. A Path turns the geometry into a visual representation on the screen, so here you can set the color and stroke thickness.

Your problem is at the very last step, you are setting the content property of your control. Typically this property is used to associate some data object with a control, you then supply a template which is its visual representation.

What you need to do is add your paths as children of a panel.

For example, add a Canvas, or a Grid to your control. Then add your two paths to the Grid / Canvas via their Children collection property.

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