2017-06-05 70 views
1

我在應用程序中使用System.Windows.Shapes.Line在畫布上繪製一條線。我想放一個形狀,例如在線的開始和結束處有一個十字('x')?有沒有辦法通過設置一個屬性來做到這一點。我可以根據座標在畫布上添加'x',但我希望能夠直接使用某些Line屬性來完成此操作。 目前我能夠使用屬性來繪製此= --------------- dashed line爲下段: -在WPF中創建行和行尾的形狀

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

如何在行尾製作自定義形狀?

+2

還有就是StrokeStartLineCap和StrokeEndLineCap屬性:https://msdn.microsoft.com/en-us/library/ms754071(v=vs.110).aspx – mm8

+0

@ mm8我已經在使用StrokeStartLineCap和StrokeEndLineCap(正如你在編輯的問題中看到的那樣)。 –

+0

「PenLineCap的4個枚舉似乎沒有達到目的」是什麼意思?檢查文檔中的示例。 – mm8

回答

1

評論建議,並鼓勵我寫我自己的形狀放在線帽。這可能不是最好的方法,但對我很好。該類將一個對象作爲一個Grid返回,我可以在繪製線條時將其添加到畫布中。這就是我已經做到了: -

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