Question

I have a property on my view model which returns some hard coded test data:

public ObservableCollection<Item> Items
{
  get
  {
    return new ObservableCollection<Item> {
      new Item { Key = 0, Count = 5 },
      new Item { Key = 1, Count = 3 },
      new Item { Key = 2, Count = 2 },
      new Item { Key = 3, Count = 7 }
    }
  }
}

My chart is defined in the view as:

<toolkit:WrapPanel>
  <toolkit:Chart>
    <toolkit:LineSeries ItemsSource="{Binding Items}" IndependentAxis="{Binding Key}" DependentValueBinding="{Binding Count}"/>
  </toolkit:Chart>
</toolkit:WrapPanel>

With that code, I get the following exception:

System.Windows.Data Error: BindingExpression path error: 'Key' property not found on 'Test.MyViewModel' 'Test.MyViewModel' (HashCode=50157845). BindingExpression: Path='Key' DataItem='Test.MyViewModel' (HashCode=50157845); target element is 'System.Windows.Controls.DataVisualization.Charting.LineSeries' (Name=''); target property is 'IndependentAxis' (type 'System.Windows.Controls.DataVisualization.Charting.IAxis').

If I bind the DataContext of the chart to Items, then no exception is thrown, but the chart shows no data.

edit: No, it complains about not being able to bind Key and Count on the collection instead.

All of the examples I've seen have their charts set up like this, so why is mine attempting to bind to the view model? Or rather, how come this appears to work for anyone else?

Was it helpful?

Solution

You probably need to use IndependentValueBinding="{Binding Key}".

IndependentAxis specifies what type of axis you want to use - categorical, date/time, etc., not where to get the data for the axis:

<charting:LineSeries.IndependentAxis>
   <charting:CategoryAxis Orientation="X"/>
</charting:LineSeries.IndependentAxis>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top