2015-05-25 43 views
0

我想動態地創建圖表woth wpf工具包,而不使用xmal來綁定依賴和獨立的值;這個代碼不工作用wpf工具包動態創建圖表

  Chart myChart = new Chart(); 

      BarSeries colser = new BarSeries(); 
      myChart.Width = 250; 
      myChart.Height = 250; 
      myChart.Title = "chart"; 

      List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>(); 
      valueList.Insert(0, new KeyValuePair<string, int>("TOR", 10)); 
      valueList.Insert(1, new KeyValuePair<string, int>("SOC", 15)); 
      valueList.Insert(2, new KeyValuePair<string, int>("BOS", 18)); 
      valueList.Insert(3, new KeyValuePair<string, int>("NON", 11)); 

      colser.Title = "title"; 
      colser.IndependentValuePath = "Value"; 
      colser.DependentValuePath = "Key"; 

      colser.ItemsSource = valueList; 

      myChart.Series.Add(colser); 
      // added myChart to stackpanel as child 

回答

0

剛剛發現的slution我的問題

var barSeries = new BarSeries 
       { 
          Title = "Fruit in Cupboard", 
          ItemsSource = new[] 
               { 
                new KeyValuePair<string, int>("TOR", 10), 
                new KeyValuePair<string, int>("SOC", 15), 
                new KeyValuePair<string, int>("BOS", 18), 

                new KeyValuePair<string, int>("NON", 11) 
               }, 
          IndependentValueBinding = new Binding("Key"), 
          DependentValueBinding = new Binding("Value") 
         }; 

    myChart.Series.Add(barSeries);