Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
25
Databinding Linechart to a collection (and dynamically changing the binding)
posted

Hello,

I can databind linechart to custom IEnumerable collection, this is done with ObjectDataProvider in XAML like this:

In <Window.Resources>: 

<ObjectDataProvider x:Key="PatientData" ObjectType="{x:Type local:PatientDataReader}" MethodName="GetData"/>

 

In <XamChart>: 

<igCA:Series ChartType="Line"

StrokeThickness="3"

DataSource="{Binding Source={StaticResource PatientData}}"

DataMapping="Label=Name; Value=Age">

</igCA:Series>

 

However, now need to change the binding in runtime, it requires code in C# code-behind file. 

How is this done? Should I use DataContext or what?

-pom- 

 

  • 25
    posted

    Ha! Sometimes one just comes up with the solution right after the question is asked.

    This is the solution in the code behind:

     

    ObjectDataProvider provider = (ObjectDataProvider)this.FindResource("PatientData");

    provider.MethodName = "GetData2";

     

    This will dynamically change the method on the bound object to another! Not actually XamChart feature but a general WPF feature.

     

    -pom-

  • 739
    posted

    Hi,

    To change data source in runtime DataSource and DataMapping properties have to be set. The DataSource property just needs a reference to your collection in runtime:

    private void Button1_Click(object sender, RoutedEventArgs e)

    {          
       this.xamChart1.RefreshEnabled = false;          
       this.xamChart1.DataSource = myCollection;          
       this.xamChart1.DataMapping=” Label=Name; Value=Age”;       
       this.xamChart1.RefreshEnabled = true;

    }

    Thanks,
    GoranS