This is a migrated thread and some comments may be shown as answers.

Binding a SeriesMapping to the viewModel programmatically

1 Answer 91 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 11 Mar 2015, 10:11 AM
Hi,

I'm having some difficulties adding a binding to my radChart programmatically. When using the following XAML everything works as intended and the chart responds to changes to the underlying data:

<telerik:RadChart.SeriesMappings>
    <telerik:SeriesMapping LegendLabel="test" ItemsSource="{Binding MyData}">
        <telerik:SeriesMapping.SeriesDefinition>
            <telerik:StackedBarSeriesDefinition />
        </telerik:SeriesMapping.SeriesDefinition>
        <telerik:ItemMapping DataPointMember="YValue" FieldName="Length" />
        <telerik:ItemMapping DataPointMember="XCategory" FieldName="Name" />
    </telerik:SeriesMapping>
</telerik:RadChart.SeriesMappings>

However I need to add the series mappings dynamically, so I tried the following approach:

private SeriesMapping createSeries(string itemsSourceBindingPath, string legendLabel)
{
    SeriesMapping series = new SeriesMapping()
    {
        LegendLabel = legendLabel,
        SeriesDefinition = new HorizontalStackedBarSeriesDefinition()
    };
 
    series.ItemMappings.Add(itemMapping1);
    series.ItemMappings.Add(itemMapping2);
 
    BindingOperations.SetBinding(series, SeriesMapping.ItemsSourceProperty, new Binding(itemsSourceBindingPath));
 
    return series;
}

I then add the series to my chart like this:
myChart.SeriesMappings.Add(createSeries("MyData", "Stuff"));
myChart.SeriesMappings.Add(createSeries("MyData2", "More stuff"));
myChart.SeriesMappings.Add(createSeries("MyData2", "Other stuff"));

But after doing this the chart is not responding to changes to the data like it did when I manually added the series mappings in the XAML. Any ideas on where the issue lies?

Thanks,
Jonathan

1 Answer, 1 is accepted

Sort by
0
Accepted
Peshito
Telerik team
answered on 13 Mar 2015, 11:56 AM
Hello,

I tried to recreate your scenario and had no issues with setting the ItemsSource binding in code behind as you mentioned. Attached is the sample I worked on. I guess the you are having this issue as a result of improper implemented Property Changed Notification.

Please take a look at the attached example in order to see how to properly set it up. Hope this helps.

Furthermore, you are using the older charting control and I would suggest you in case you are just starting with the chart to give a try of the newer one - ChartView.

Regards,
Peshito
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Chart
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Peshito
Telerik team
Share this question
or