I’ve a WPF toolkit chart which I want to be able to both specify the axis range and change the data being shown. Either of these work fine independently, but when I put them together I get an exception thrown from the binding code because the axis minimum and maximum are briefly reversed.
Here’s some code:
<Common:MultiSeriesChart SeriesSource="{Binding Data}" >
<chartingToolkit:Chart.Axes>
<chartingToolkit:LinearAxis
Orientation="X" Minimum="{Binding MinimumMass}" Maximum="{Binding MaximumMass}"/>
<chartingToolkit:LinearAxis
Orientation="Y" Minimum="0" Maximum="100"/>
</chartingToolkit:Chart.Axes>
</Common:MultiSeriesChart>
And the code in the ViewModel (fairly standard MVVM stuff):
private double maximumMass;
public double MaximumMass
{
get { return maximumMass; }
set
{
if (maximumMass != value)
{
maximumMass = value;
RaisePropertyChanged(() => MaximumMass);
}
}
}
private double minimumMass;
public double MinimumMass
{
get { return minimumMass; }
set
{
if (minimumMass != value)
{
minimumMass = value;
RaisePropertyChanged(() => MinimumMass);
}
}
}
When I change the data being drawn I need to update the MinimumMass and MaximumMass for the X axis. First I update the MinimumMass to the new value, then the MaximumMass. This is fine unless the new MinimumMass value is bigger than the old MaximumMass, at which point the binding code in the graph throws an InvalidOperationException: The minimum value must be smaller than or equal to the maximum value.
I could update them in the opposite order, but then I’d get the same problem in reverse. Does anyone know the correct way to deal with this without a horrible kludge to ensure the maximum is temporarily too big to be beaten (i.e. set maximum to double.MaxValue, then set minimum to new minimum, then maximum to new maximum)?
In the end I just checked the order before assigning the new value: