绑定WPFToolkit图表时出现System.NullReferenceException

时间:2012-03-26 11:10:07

标签: mvvm wpftoolkit

一旦我的viewmodel上的属性包含数据,WPFToolkit就会抛出异常,但是当它为空时不会抛出异常!

查看:

<UserControl x:Class="ChartView"
    xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
<Grid>
    <DVC:Chart>
        <DVC:Chart.Series>
            <DVC:ScatterSeries ItemsSource="{Binding Data}"
                               DependentValueBinding="{Binding Path=Value}"
                               IndependentValueBinding="{Binding Path=Key}" />
        </DVC:Chart.Series>
    </DVC:Chart>
</Grid>

视图模型:

public class ChartViewModel  
{
    public ObservableCollection<KeyValuePair<int, int>> Data { get; set; }

    public ChartViewModel()
    {
        Data = new ObservableCollection<KeyValuePair<int, int>>();

        //If I uncomment this, the error is thrown upon binding
        //Data.Add(new KeyValuePair<int, int>(1, 10));
    }
}

堆栈跟踪的顶部是:

at System.Windows.Controls.DataVisualization.Charting.ScatterSeries.UpdateDataPoint(DataPoint dataPoint) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\Series\ScatterSeries.cs:line 179
at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.OnDataPointActualIndependentValueChanged(DataPoint dataPoint, Object oldValue, Object newValue) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\Series\DataPointSeriesWithAxes.cs:line 257
at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.OnDataPointActualIndependentValueChanged(Object sender, RoutedPropertyChangedEventArgs`1 args) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\Series\DataPointSeries.cs:line 1322
at System.Windows.Controls.DataVisualization.Charting.DataPoint.OnActualIndependentValuePropertyChanged(Object oldValue, Object newValue) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\DataPoint\DataPoint.cs:line 657
at System.Windows.Controls.DataVisualization.Charting.DataPoint.OnActualIndependentValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\DataPoint\DataPoint.cs:line 629

1 个答案:

答案 0 :(得分:1)

解决方案非常遥远。在我的App.xaml.cs中,我为了本地化而改变了语言(我希望逗号作为小数点):

using System.Windows.Markup;
using System.Globalization;


var window = new MainWindowView();
window.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
window.Show();

显然,WPFToolkit不喜欢这个。