The main thing you'll need for the binding is a converter that turns your points into Geometry
which the path will need as Data
, here is what my one-way converter from a System.Windows.Point
-array to Geometry looks like:
[ValueConversion(typeof(Point[]), typeof(Geometry))]
public class PointsToPathConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Point[] points = (Point[])value;
if (points.Length > 0)
{
Point start = points[0];
List<LineSegment> segments = new List<LineSegment>();
for (int i = 1; i < points.Length; i++)
{
segments.Add(new LineSegment(points[i], true));
}
PathFigure figure = new PathFigure(start, segments, false); //true if closed
PathGeometry geometry = new PathGeometry();
geometry.Figures.Add(figure);
return geometry;
}
else
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
Now all that is really left is to create an instance of it and use it as the converter for the binding. What it might look like in XAML:
<Grid>
<Grid.Resources>
<local:PointsToPathConverter x:Key="PointsToPathConverter"/>
</Grid.Resources>
<Path Data="{Binding ElementName=Window, Path=Points, Converter={StaticResource ResourceKey=PointsToPathConverter}}"
Stroke="Black"/>
</Grid>
If you need the binding to update automatically you should work with dependency properties or interfaces like INotifyPropertyChanged
/INotifyCollectionChanged
Hope that helps :D
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…