I want that figSpeed always reflets pSpeed but when I come to binding in onChangeX method, I get always a System.NullReferenceException
Anyone can help me? Seems the reference is correct and so is the case.
PointsToPathConverter class:
[ValueConversion(typeof(List<Point>), typeof(Geometry))]
public class PointsToPathConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
List<Point> points = (List<Point>)value;
if (points.Count > 0)
{
Point start = points[0];
List<LineSegment> segments = new List<LineSegment>();
for (int i = 1; i < points.Count; 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
}
dataProjectorVM class:
public class dataProjectorVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Path figSpeed;
public List<Point> pSpeed;
public dataProjectorVM()
{
pSpeed = new List<Point>();
pSpeed.Add(new Point(0, 0));
Binding bind;
bind = new Binding("pSpeed")
{
Source = this,
Mode = BindingMode.OneWay,
Converter = new PointsToPathConverter(),
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
figSpeed = new Path()
{
Stroke = Brushes.Black,
StrokeThickness = 1
};
figSpeed.SetBinding(Path.DataProperty, bind);
}
public void onChangeX()
{
pSpeed.Clear();
double pm = -2;
foreach (dataPacket dp in appMain.dataMgr.retrive.result)
{
double _pm = appMain.dataMgr.projector.getX(dp.pm);
if (_pm > pm + 1)
{
pm = _pm;
pSpeed.Add(new Point(pm, appMain.dataMgr.projector.getSpeedY(dp.speed)));
}
}
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("pSpeed"));
}
}
If the
PropertyChangedevent doesn’t have any handlers,this.PropertyChangedwill be null.You need to check for that.