I'm trying to make work the following code:
XAML:
<Grid x:Name="MainGrid">
<charts:Chart x:Name="chart1"/>
</Grid>
I am using
xmlns:charts="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit
CODE
using System.Windows.Controls.DataVisualization.Charting;
public MainWindow()
{
InitializeComponent();
PointCollection pc = new PointCollection();
for (int i = 0; i < 100; i++)
{
pc.Add(new System.Windows.Point { X = i, Y = i * 2 });
}
LineSeries series1 = new LineSeries();
series1.DependentValuePath = "Y";
series1.IndependentValuePath = "X";
series1.ItemsSource = pc;
chart1.Series.Add(series1);
}
But I'm getting Unsupported exception error :
Exception thrown: 'System.NotSupportedException' in PresentationFramework.dll
Additional information: Cannot convert the value in attribute 'Property' to object of type 'System.Windows.DependencyProperty'.
The error comes along with the 'No source available' screen, I am using Visual Studio 2015.
I've read that chart.series is not a dependency property, so my questions is what is the proper way of assigning values to series ?
I've read that chart.series is not a dependency property, so my questions is what is the proper way of assigning values to series ?
You should specify your series type statically in XAML and bind the points collection as its ItemsSource
:
<Grid x:Name="MainGrid">
<charts:Chart x:Name="chart1">
<charts:Chart.Series>
<charts:LineSeries IndependentValuePath="X" DependentValuePath="Y" ItemsSource="{Binding points}"/>
</charts:Chart.Series>
</charts:Chart>
</Grid>
Initialization code:
public MainWindow()
{
InitializeComponent();
PointCollection pc = new PointCollection();
for (int i = 0; i < 100; i++)
{
pc.Add(new System.Windows.Point { X = i, Y = i * 2 });
}
// in some appropriate datacontext, set some object that contains your points collection for the binding
chart1.DataContext = new { points = pc };
}
Note: this doesn't explain the errors that where mentioned in the question, it's just a way to define the chart with better View-ViewModel separation than the question approach. The question code for itself isn't enough to reproduce the error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.