将字典绑定到WPF工具包图表

时间:2011-05-16 18:36:24

标签: wpf binding dictionary charts wpftoolkit

这是我将字典项绑定到Chart控件的代码。我一直收到以下错误:

“索引超出范围。必须是非负数且小于集合的大小。参数名称:索引”

这是我的代码:

foreach (DataRow drow in objD0s.Tables[0].Rows)// Adding values from a dataset to dictionary    
     {


         string strvalue = Convert.ToString(drow["Chemical Name"]);
         string doublevalue = Convert.ToString(drow["Global Warming"]);
         Cdata.Add(Convert.ToString(strvalue), Convert.ToDouble(doublevalue));

     }
     ColumnSeries colSeries = ChartChemImpact.Series[0] as ColumnSeries;     

         ((ColumnSeries)ChartChemImpact.Series[0]).DataContext = Cdata;
         colSeries.ItemsSource = myDataTable0.DefaultView;
         colSeries.IndependentValueBinding = new Binding("[Chemical Name]");
         colSeries.DependentValueBinding = new Binding("[Global Warming]");

  }

XAM

 chartingToolkit:Chart Name="ChartChemImpact" Title="Chart Title"   Width="384"            Height="280">
   <chartingToolkit:ColumnSeries DependentValuePath="Key"  IndependentValuePath="Value" ItemsSource="{Binding}" Name="colSeries" />
  </chartingToolkit:Chart>

请帮助:(

1 个答案:

答案 0 :(得分:1)

好的,我发现了错误。 xaml中有绑定和值路径,但它们在代码中被替换。

我会删除这些行,所以结果如下:

foreach (DataRow drow in objD0s.Tables[0].Rows)// Adding values from a dataset to dictionary    
{
     string strvalue = Convert.ToString(drow["Chemical Name"]);
     string doublevalue = Convert.ToString(drow["Global Warming"]);
     Cdata.Add(Convert.ToString(strvalue), Convert.ToDouble(doublevalue));
}

((ColumnSeries)ChartChemImpact.Series[0]).DataContext = Cdata;
//And that's the end of the function, no more code
}