1

我必须在我的视图模型类上遵循字典

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="{Binding SpendingCategories}"有问题,但我似乎看不到它。

任何帮助将非常感激。

4

1 回答 1

2

因为 BindingSpendingCategories必须是公共属性。

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

public SpendingCategoriesViewModel()
{
    SpendingCategories = new Dictionary<string,decimal>
    {   
        {"Petrol", 120.5m},
        {"Rent", 400},
        {"Food", 200}
    };
}
于 2012-07-17T09:53:00.200 回答