2010-07-07 109 views
1

我是新來的Silverlight,我試圖在圖形顯示詞典內容:綁定字典到圖表

在代碼隱藏:

ChartData = new Dictionary<DateTime, double> {{DateTime.Now, 10}, 
     {DateTime.Now, 20}, {DateTime.Now, 15}}; 

而在Silverlight的XAML:

<toolkit:Chart HorizontalAlignment="Left" Margin="113,168,0,0" Name="chart1" 
    Title="Chart Title" VerticalAlignment="Top"> 
    <toolkit:LineSeries ItemsSource="{Binding Path=ChartData}" 
      DependentValuePath="Key" IndependentValuePath="Value"> 
    </toolkit:LineSeries> 
</toolkit:Chart> 

但是這給出了「沒有合適的座標軸可用於繪製相關值」。建議?

回答

2

試試這個數據集: -

ChartData = new Dictionary<DateTime, double>() { 
    { DateTime.Now.AddDays(-1), 10 }, 
    { DateTime.Now, 20 }, 
    { DateTime.Now.AddDays(1), 15 } 
}; 

(我驚訝你的代碼行甚至工作,因爲它會試圖使用相同的值添加多個鍵進入字典)。

然後更改您的XAML: -

<toolkit:LineSeries ItemsSource="{Binding Path=ChartData}"  
     DependentValuePath="Value" IndependentValuePath="Key"> 

它是極不尋常的使用日期作爲DependentValue,其實我想不出一個場景,其中一個DependentValue會以外的任何其他數字。

+0

一個小的補充:對鍵/值綁定使用'DependentValueBinding =「{Binding Path = Value}」IndependentValueBinding =「{Binding Path = Key}」''。否則,他們不會工作:) – Deruijter 2012-06-27 07:50:03

+1

@Deruijter:這是什麼'xxxxxValuePath'屬性最終做?那就是在'xxxxxValueBinding'屬性上設置Binding。 – AnthonyWJones 2012-06-27 11:51:42

+0

安東尼,現在你提到它,我沒有注意到你使用了Path而不是Binding!你的方法實際上是完美的。我已經在使用'xxxxValueBinding',所以在閱讀你的答案時,我下意識地認爲你也是這樣做的。說實話,我不確定'xxxxValuePath'是否在屏幕後面設置了'xxxxValueBinding'(我還沒有xaml專家)。但我認爲這很有可能。 – Deruijter 2012-06-27 14:40:55