该错误发生在更复杂的上下文中,但可以在这个简单的示例中重现:
主窗口.xaml
<Window>
<StackPanel>
<Button Click="Button_Click_1">Clear</Button>
<Button Click="Button_Click_2">Modify</Button>
<charting:Chart x:Name="chart" />
</StackPanel>
</Window>
主窗口.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);
}
}
单击清除,然后单击修改。清除从图表中删除系列并创建一个新系列。Modify 修改系列绑定的源。UpdateDataPoint
我得到 NullReferenceException的已删除系列调用ActualDependentRangeAxis
为空:
protected override void UpdateDataPoint(DataPoint dataPoint)
{
double maximum = ActualDependentRangeAxis.GetPlotAreaCoordinate(
ActualDependentRangeAxis.Range.Maximum).Value;