这个错误发生在一个更复杂的环境中,但可以在这个简单的例子中复现:
MainWindow.xaml
<Window>
<StackPanel>
<Button Click="Button_Click_1">Clear</Button>
<Button Click="Button_Click_2">Modify</Button>
<charting:Chart x:Name="chart" />
</StackPanel>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
Random rand = new Random();
ObservableCollection<KeyValuePair<double, double>> values =
new ObservableCollection<KeyValuePair<double, double>>();
public MainWindow()
{
InitializeComponent();
values.Add(new KeyValuePair<double, double>(10, 10));
values.Add(new KeyValuePair<double, double>(20, 40));
values.Add(new KeyValuePair<double, double>(30, 90));
values.Add(new KeyValuePair<double, double>(40, 160));
values.Add(new KeyValuePair<double, double>(50, 250));
AddSeries();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
chart.Series.Clear();
AddSeries();
}
private void AddSeries()
{
var series = new LineSeries();
series.SetBinding(LineSeries.ItemsSourceProperty, new Binding());
series.DataContext = values;
series.DependentValueBinding = new Binding("Value");
series.IndependentValueBinding = new Binding("Key");
chart.Series.Add(series);
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
values[3] = new KeyValuePair<double,double>(40, rand.NextDouble() * 300);
}
}
点击清除,然后点击修改。清除将从图表中删除该系列并创建一个新系列。修改将修改系列绑定的源。已删除的系列调用UpdateDataPoint
,其中我遇到了空引用异常:ActualDependentRangeAxis
为空:
protected override void UpdateDataPoint(DataPoint dataPoint)
{
double maximum = ActualDependentRangeAxis.GetPlotAreaCoordinate(
ActualDependentRangeAxis.Range.Maximum).Value;
我使用的是数据可视化开发版本4.0,具体内容请参考此链接