我必须在我的视图模型类
上跟踪字典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>
没有显示任何数据。 如果我在views类中手动设置了该系列的ItemsSource,它确实会显示数据,所以我假设 ItemsSource =“{Binding SpendingCategories}”有问题,但我似乎无法看到它。
非常感谢任何帮助。
答案 0 :(得分:2)
对于绑定SpendingCategories
必须是公共财产。
public Dictionary<string, decimal> SpendingCategories { get; private set; }
public SpendingCategoriesViewModel()
{
SpendingCategories = new Dictionary<string,decimal>
{
{"Petrol", 120.5m},
{"Rent", 400},
{"Food", 200}
};
}