Silverlight 4.0 在silverlight中从代码隐藏中定义路径数据

Silverlight 4.0 在silverlight中从代码隐藏中定义路径数据,silverlight-4.0,c#-4.0,Silverlight 4.0,C# 4.0,我有以下路径数据,它是xaml格式的。我想从代码隐藏中定义相同的路径数据 <Path Data="M 250,40 L200,20 L200,60 Z" /> 您需要使用类型转换器: Path path = new Path(); string sData = "M 250,40 L200,20 L200,60 Z"; var converter = TypeDescriptor.GetConverter(typeof(Geometry)); path.Data = (Geome

我有以下路径数据,它是xaml格式的。我想从代码隐藏中定义相同的路径数据

<Path  Data="M 250,40 L200,20 L200,60 Z" />

您需要使用
类型转换器

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);
来自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;
编辑:

//我们必须将其设置为真,才能从lineSegment2到起点绘制直线

pathFigure.IsClosed = true;

免责声明:我只是将路径作为数据模板作为列表框来完成这项工作。应该有用

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

问题是关于Silverlight的,所以这不适用。还有
path.SetBinding(path.DataProperty,new Binding(){Source=“M 250,40 L200,20 L200,60 Z”})