'LibeCharts WPF - binding to a Series
I am struggling in very beginning with WPF and LiveCharts.
I am trying to implement the Starting Example (https://lvcharts.net/App/examples/v1/Wpf/Components) but couldn't understand how it works (or better to say not working for me) and there to to place the snippet into a WPF project.
With XAML it is ok, I just put the example row into the Grid of MainWindow.xaml and while debugging it shows the element
<lvc:CartesianChart Series="{Binding SeriesCollection}" />
But then I did not get into C# part
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Values = new ChartValues<double> { 3, 5, 7, 4 }
},
new ColumnSeries
{
Values = new ChartValues<decimal> { 5, 6, 2, 7 }
}
};
When I add it to public partial class MainWindow it is interpreted wrong.
And I do not understand what happen in the first row:
SeriesCollection = new SeriesCollection
As I understand, we are to create an object with a constructor which add LineSeries and ColumnSeries values to an object of class SeriesCollection.
So it shold be like
SeriesCollection mySeries = new SeriesCollection()
This one is compiling properly, but anyway I couldn't get a binding to mySeries object:
No DataContext found for binding 'mySeries' for my "updated" program
No DataContext found for binding 'SeriesCollection' for initital example
Solution 1:[1]
It appears the binding requires a get/set e.g.
public SeriesCollection SeriesCollection { get; set; }
You also require the DataContext property set in your constructor:
DataContext = this;
Otherwise the binding fails silently at runtime.
It appears the getting started guide is broken at https://lvcharts.net/App/examples/v1/wpf/Components. I had to use the Wayback Machine to find a working copy of the getting started guide: https://web.archive.org/web/20190717052904/https://lvcharts.net/App/examples/v1/wpf/Basics
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Daniel Ellis |
