ColumnSeries使用WPF Toolkit异常绑定数据

时间:2015-11-30 16:43:50

标签: c# wpf xaml wpftoolkit

我使用WPF Toolkit扩展名来显示一个列图,该列图从Dictionary<string,ulong>获取数据(其中字符串是X值,而ul是Y值)。这是XAML的声明

<chartingToolkit:Chart x:Name="Chart" HorizontalAlignment="Left" Margin="10,10,0,0" Title="Chart Title" VerticalAlignment="Top" Height="560" Width="1000">
    <chartingToolkit:ColumnSeries x:Name="myGraph" DependentValuePath="Key" IndependentValuePath="Value" ItemsSource="{Binding}"/>
</chartingToolkit:Chart>

我有一个构造Dictionary <string,ulong>

的对象

所以,我宣布了全局变量

private Dictionary<string,ulong> myDictionary;

存储来自我的对象的数据并将其与图表绑定,如下所示

this.myDictionary = myObject.objectDictionary;
myGraph.Title = "My Title";
myGraph.ItemsSource = this.myDictionary;

但我得到以下例外:

Object reference not set to an instance of an object.

那么,我是否对绑定做错了?

此致

1 个答案:

答案 0 :(得分:0)

好的,我在程序中发现了错误。我使用ItemsSource正确定义myGraph.DataContextIndependentValuePath,而非使用DependentValuePath

this.myDictionary = myObject.objectDictionary;
myGraph.DataContext = this.myDictionary;
myGraph.IndependentValuePath = "Key";
myGraph.DependentValuePath = "Value";