Tôi có dữ liệu đường dẫn dưới đây có trong xaml. Tôi muốn xác định cùng một dữ liệu đường dẫn từ mã phía sau.Xác định dữ liệu đường dẫn từ mã phía sau trong silverlight
<Path Data="M 250,40 L200,20 L200,60 Z" />
Tôi có dữ liệu đường dẫn dưới đây có trong xaml. Tôi muốn xác định cùng một dữ liệu đường dẫn từ mã phía sau.Xác định dữ liệu đường dẫn từ mã phía sau trong silverlight
<Path Data="M 250,40 L200,20 L200,60 Z" />
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;
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);
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);
}
và cũng 'path.SetBinding (Path.DataProperty, new Binding() {Source =" M 250,40 L200,20 L200,60 Z "});' –
Câu hỏi đặt ra là về Silverlight, do đó, điều này không áp dụng. – Recep