JKF
JKF

Reputation: 216

Binding a dictionary to wpf toolkit chart

I have to following dictionary on my view model class

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

        {"Petrol", 120.5m},

        {"Rent", 400},

        {"Food", 200}
  };
}

and the following XAML code

<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>

The isn't displaying any data. If I set the ItemsSource for the series manually in the views class it does show the data so I'm assuming there is something wrong with ItemsSource="{Binding SpendingCategories}" but I can't seem to see it.

Any help would be much appreciated.

Upvotes: 1

Views: 1091

Answers (1)

LPL
LPL

Reputation: 17083

For Binding SpendingCategories has to be a public Property.

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

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

Upvotes: 2

Related Questions