ColumnSeries 绑定数据与 WPF 工具包异常
ColumnSeries binding data with WPF Toolkit exception
我正在使用 WPF Toolkit
扩展来显示从 Dictionary<string,ulong>
中获取数据的柱形图(其中字符串是 X 值,ulong 是 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.
那么,是不是我的绑定有问题?
此致
好的,我在我的程序中发现了错误。我没有使用 ItemsSource
,而是使用 myGraph.DataContext
正确定义了 IndependentValuePath
和 DependentValuePath
。
this.myDictionary = myObject.objectDictionary;
myGraph.DataContext = this.myDictionary;
myGraph.IndependentValuePath = "Key";
myGraph.DependentValuePath = "Value";
我正在使用 WPF Toolkit
扩展来显示从 Dictionary<string,ulong>
中获取数据的柱形图(其中字符串是 X 值,ulong 是 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.
那么,是不是我的绑定有问题?
此致
好的,我在我的程序中发现了错误。我没有使用 ItemsSource
,而是使用 myGraph.DataContext
正确定义了 IndependentValuePath
和 DependentValuePath
。
this.myDictionary = myObject.objectDictionary;
myGraph.DataContext = this.myDictionary;
myGraph.IndependentValuePath = "Key";
myGraph.DependentValuePath = "Value";