【问题标题】:Put a shape at the start and end of a line in WPF在 WPF 中的一行的开头和结尾放置一个形状
【发布时间】:2017-06-05 10:41:39
【问题描述】:

我在我的应用程序中使用 System.Windows.Shapes.Line 在画布上绘制一条线。我想放置一个形状,例如行首和行尾的十字('x')?有没有办法通过设置属性来做到这一点。我可以根据坐标在画布上添加一个“x”,但我希望我们可以直接使用一些 Line 属性来做到这一点。 目前我可以画这个= ---------------- dashed line 使用sn-p下的属性:-

var DistanceLine = new Line();
DistanceLine.Stroke = new SolidColorBrush(LineColor);
DistanceLine.StrokeDashArray = new DoubleCollection() {0, 4};
DistanceLine.StrokeDashCap = PenLineCap.Round;
DistanceLine.StrokeEndLineCap = PenLineCap.Round;
DistanceLine.StrokeLineJoin = PenLineJoin.Round;
DistanceLine.StrokeStartLineCap = PenLineCap.Round;
DistanceLine.StrokeThickness = 3;

我想要这样的东西 = x-----------------x dashed line with 'x' marks

如何在行尾制作自定义形状?

【问题讨论】:

  • 有 StrokeStartLineCap 和 StrokeEndLineCap 属性:msdn.microsoft.com/en-us/library/ms754071(v=vs.110).aspx
  • @mm8 我已经在使用 StrokeStartLineCap 和 StrokeEndLineCap(正如您在编辑后的问题中看到的那样)。
  • “PenLineCap 的 4 个枚举似乎没有达到目的”是什么意思?检查文档中的示例。
  • @mm8 我的意思是 PenLineCap 的 4 个枚举具有基于笔画粗细的形状。我无法弄清楚在行尾放置自定义形状(如附图所示)。不幸的是,在 so 上找不到类似的问题。
  • 当然,如文档中所述,它们“服务于目的”。尝试增加厚度并去掉 dash 阵列,您会清楚地看到平盖和圆盖之间的区别。

标签: c# wpf canvas line stroke


【解决方案1】:

cmets 建议并鼓励我写出自己的形状来放置线帽。这可能不是最好的方法,但对我很有效。该类返回一个对象作为网格,当我画一条线时,我可以将其添加到我的画布中。我就是这样做的:-

public class CrossHair : Grid
    {
        public string LineName { get; set; }

        /// <summary>
        /// Draws the crosshair at the given point
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        static public CrossHair DrawCrossHair(double x, double y)
        {
            var crosshair = new CrossHair(); // to contain the cross hair
            var line1 = new Line();
            var line2 = new Line();
            var line3 = new Line();
            var line4 = new Line();
            line1.Stroke = line2.Stroke = line3.Stroke = line4.Stroke = new SolidColorBrush(Colors.Yellow);
            line1.StrokeThickness = line2.StrokeThickness = line3.StrokeThickness = line4.StrokeThickness = 2;

            line1.X1 = x - 5;
            line1.Y1 = y;
            line1.X2 = x - 2;
            line1.Y2 = y;

            line2.X1 = x;
            line2.Y1 = y + 5;
            line2.X2 = x;
            line2.Y2 = y + 2;

            line3.X1 = x + 2;
            line3.Y1 = y;
            line3.X2 = x + 5;
            line3.Y2 = y;

            line4.X1 = x;
            line4.Y1 = y - 2;
            line4.X2 = x;
            line4.Y2 = y - 5;

            crosshair.Children.Add(line1);
            crosshair.Children.Add(line2);
            crosshair.Children.Add(line3);
            crosshair.Children.Add(line4);

            return crosshair;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    相关资源
    最近更新 更多