Greg Harrison
Greg Harrison

Reputation: 29

Having trouble binding data from C# to WPF to draw a line graph

Code Behind - I have an Observable Collection of Points which holds data to plot to the graph. This data is read from an external source and will have anything from 20k-80k thousands values in each of these lists.

ObservableCollection<System.Windows.Point> listChannelData =new ObservableCollection <System.Windows.Point>();
ObservableCollection<System.Windows.Point> listActiveBaseline =new ObservableCollection <System.Windows.Point>();

Once these lists are populated, I then set the series ItemSource to these lists like so:

seriesChannelData.ItemsSource = listChannelData;
seriesActiveBaseline.ItemsSource = listActiveBaseline;

XAML - Not sure if the XAML is correct as just started learning, I also don't know if it's better to bind to the Observable Collection in the code or in the XAML.

<DVC:LineSeries Name="seriesChannelData" DependentValuePath="Y" IndependentValuePath="X" ItemsSource="{Binding seriesChannelData}" IsSelectionEnabled="True"/>
<DVC:LineSeries Name="seriesActiveBaseline" DependentValuePath="Y" IndependentValuePath="X" ItemsSource="{Binding}" IsSelectionEnabled="True" />

When I try to run this program it seems to take a long time to populate the lists and nothing charts, so it seems my data binding is wrong. Another thing I would like to find out is if I am wrong in setting the series item source to the lists after they have been populated.

Upvotes: 1

Views: 262

Answers (1)

Kiran
Kiran

Reputation: 11

<DVC:Chart Name="seriesChannelData" VerticalAlignment="Top" Height="200"
DataContext="{Binding seriesChannelData}" IsTabStop="True" Background="White">
    <DVC:LineSeries DependentValuePath="Y" IndependentValuePath="X"
ItemsSource="{Binding}">
    </DVC:LineSeries>
</DVC:Chart>

In the DataContext, you will bind the observable collection Name and, in the DependentValuePath and IndependentValuePath, you bind the members of the observable collection.

Upvotes: 1

Related Questions