WPF: GraphHost problem when binding to a ViewModel

Jul 14, 2009 at 9:44 PM
Edited Jul 14, 2009 at 9:45 PM

If the current page/usercontrol/window is binded to a ViewModel, as is the case when working with mvvm, the GraphHost will attempt to read the data series from its current DataContext which

happens to be the view model, since the view model probably dosen't implement the IEnumerable interface, an Object reference exception will be thrown, the code in question is found in TimeGraphBase.cs (line 1602):

            if (this.DataContext != null)
            {
                IEnumerable dataSeries = this.DataContext as IEnumerable;
                foreach (object o in dataSeries)
                {
                    gp = this.GetBoundGraphPoint(o);
                    if (gp.Y1 < minValue || minValue == -1)
                    {
                        minValue = gp.Y1;
                    }
To solve this problem you would have to either set the DataContext of the GraphHost to null from the XAML, as in:

DataContext="{x:Null}"

Or you can actually fix the method inside the GraphHost as:

            if (this.DataContext != null && DataContext is IEnumerable)
            {
                IEnumerable dataSeries = this.DataContext as IEnumerable;
                foreach (object o in dataSeries)
Let me know what you think about this, and if this is the best way to get around it.
Thanks for the good work on the controls!