Search code examples
wpfbindingchartsdataviewtoolkit

WPF Toolkit: Binding Chart to dataview using object data provider


I'm trying to create a chart using a Dataview that is returned from a DataSet Table. I created a data provider object in C# that uses a dataset and table adapter created from the datasource wizard. the Data provider object has a method that returns the default view on the table once it's filled.

The data retrieval part works fine, I'm able to see the data if I bind a datagrid to it. My question is, how do I bind the dataview to a column chart (or any chart)? My independent data is a text column in the dataview and the dependent data is a numerical amount. When I run this code, I get a runtime error like this:

Set property 'System.Windows.Controls.DataVisualization.Charting.DataPointSeries.DependentValueBinding' threw an exception.' Line number '17' and line position '18'.

What am I doing wrong?

<Window xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"  xmlns:my="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"   x:Class="GraphingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:GraphingTest"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <ObjectDataProvider ObjectType="{x:Type local:CustomDataProvider}"  x:Key="odp1">            
        </ObjectDataProvider>
        <ObjectDataProvider ObjectInstance="{StaticResource odp1}" MethodName="GetQualitativeData" x:Key="odp2">

        </ObjectDataProvider>
    </Window.Resources>
    <ScrollViewer>
    <StackPanel DataContext="{Binding Source={StaticResource odp2}}">
            <chartingToolkit:Chart  >
                <chartingToolkit:ColumnSeries DependentValueBinding="MyNumberValue" IndependentValueBinding="MyTextLabel" ItemsSource="{Binding}">

                </chartingToolkit:ColumnSeries>

            </chartingToolkit:Chart>
        </StackPanel>
    </ScrollViewer>
</Window>

Here's my C# data provder object

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GraphingTest.MyDataSetTableAdapters;

namespace GraphingTest
{
    public class CustomDataProvider
    {
        // construct the dataset
        MyDataSet dataset;

        MyDataTableAdapter adapter;

        public CustomDataProvider()
        {

            dataset = new MyDataSet();
            adapter = new MyDataTableAdapter();
            // use a table adapter to populate the Customers table
            adapter.Fill(dataset.MyData);
        }

        public DataView GetQualitativeData()
        {


            // use the Customer table as the DataContext for this Window
            var x= dataset.MyData.DefaultView;
            return x;
        }
    }
}

Solution

  • <chartingToolkit:ColumnSeries DependentValueBinding="MyNumberValue" IndependentValueBinding="MyTextLabel" ItemsSource="{Binding}"> 
    

    Should probably be:

    <chartingToolkit:ColumnSeries DependentValueBinding="{Binding MyNumberValue}" IndependentValueBinding="{Binding MyTextLabel}" ItemsSource="{Binding}"> 
    

    OR

    <chartingToolkit:ColumnSeries DependentValuePath="MyNumberValue" IndependentValuePath="MyTextLabel" ItemsSource="{Binding}">