Binding a SeriesMapping to the viewModel programmatically

2 posts, 1 answers
  1. Jonathan
    Jonathan avatar
    15 posts
    Member since:
    Dec 2014

    Posted 11 Mar 2015 Link to this post

    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
  2. Answer
    Peshito
    Admin
    Peshito avatar
    496 posts

    Posted 13 Mar 2015 Link to this post

    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.

     
  3. DevCraft banner
Back to Top