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

时间:2012-07-17 09:48:40

标签: c# .net 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>

没有显示任何数据。 如果我在views类中手动设置了该系列的ItemsSource,它确实会显示数据,所以我假设 ItemsSource =“{Binding SpendingCategories}”有问题,但我似乎无法看到它。

非常感谢任何帮助。

1 个答案:

答案 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}
    };
}