2012-07-17 86 views
1

我不得不以下詞典對我的視圖模型類綁定字典來WPF工具包圖表

public class SpendingCategoriesViewModel 
{ 
    public Dictionary<string, decimal> SpendingCategories = new Dictionary<string,decimal> 
    { 

     {"Petrol", 120.5m}, 

     {"Rent", 400}, 

     {"Food", 200} 
    }; 
} 

和下面的XAML代碼

<Grid> 
    <chartingToolkit:Chart Name="chartExpenses" Title="Expenses by Category" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> 
     <chartingToolkit:Chart.Series> 
      <chartingToolkit:PieSeries Title="Category" ItemsSource="{Binding SpendingCategories}" IndependentValuePath="Key" DependentValuePath="Value"> 
      </chartingToolkit:PieSeries> 
     </chartingToolkit:Chart.Series> 
    </chartingToolkit:Chart> 
</Grid> 

的不顯示任何數據。 如果我在視圖類它確實表明了數據,所以我假設手動設置的ItemsSource爲系列有什麼毛病的ItemsSource =「{結合SpendingCategories}」,但我似乎無法看到它。

任何幫助將不勝感激。

回答

2

爲了結合SpendingCategories必須是公共財產。

public Dictionary<string, decimal> SpendingCategories { get; private set; } 

public SpendingCategoriesViewModel() 
{ 
    SpendingCategories = new Dictionary<string,decimal> 
    { 
     {"Petrol", 120.5m}, 
     {"Rent", 400}, 
     {"Food", 200} 
    }; 
} 
+0

這是對的,我忘了這一點,謝謝。 – JKF 2012-07-17 09:55:49