2010-08-16 35 views

Trả lời

16

từ codebehind:

Path orangePath = new Path(); 

     PathFigure pathFigure = new PathFigure(); 

     pathFigure.StartPoint = new Point(250, 40); 

     LineSegment lineSegment1 = new LineSegment(); 
     lineSegment1.Point = new Point(200, 20); 
     pathFigure.Segments.Add(lineSegment1); 

     LineSegment lineSegment2 = new LineSegment(); 
     lineSegment2.Point = new Point(200, 60); 
     pathFigure.Segments.Add(lineSegment2); 

     PathGeometry pathGeometry = new PathGeometry(); 
     pathGeometry.Figures = new PathFigureCollection(); 

     pathGeometry.Figures.Add(pathFigure); 

     orangePath.Data = pathGeometry; 

Edit:

// chúng ta cần phải thiết lập đúng này để vẽ dòng từ lineSegment2 đến điểm bắt đầu

pathFigure.IsClosed = true; 
11

Bạn cần phải sử dụng một TypeConverter:

Path path = new Path(); 
string sData = "M 250,40 L200,20 L200,60 Z"; 
var converter = TypeDescriptor.GetConverter(typeof(Geometry)); 
path.Data = (Geometry)converter.ConvertFrom(sData); 
+1

Câu hỏi đặt ra là về Silverlight, do đó, điều này không áp dụng. – Recep

2

Tuyên bố từ chối: Tôi chỉ thực hiện việc này với Đường dẫn dưới dạng DataTemplate làm hộp danh sách. Nên làm việc.

//of course the string could be passed in to a constructor, just going short route. 
public class PathData 
{ 
    public string Path { get { return "M 250,40 L200,20 L200,60 Z"; } } 
} 

void foo() 
{ 
    var path = new Path() { Stroke = new SolidColorBrush(Colors.Black) }; 
    var data = new PathData(); 
    var binding = new Binding("Path") { Source=data, Mode=BindingMode.OneWay }; 
    path.SetBinding(Path.DataProperty, binding); 
} 
+1

và cũng 'path.SetBinding (Path.DataProperty, new Binding() {Source =" M 250,40 L200,20 L200,60 Z "});' –

Các vấn đề liên quan