We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to bind Series from MVVM pattern in Xamarin.Forms Chart?

Platform: Xamarin.Forms |
Control: SfChart |
Published Date: July 31, 2019 |
Last Revised Date: August 21, 2019

You can bind the Series property of SfChart from MVVM pattern by using ChartSeriesCollection and setting the ViewModel as BindingContext.

 

The following code sample demonstrates how to bind the Series property from MVVM pattern.

Xaml

<ContentPage.BindingContext>
        <local:ViewModel/>
</ContentPage.BindingContext>
 
<chart:SfChart HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Series="{Binding SeriesCollection}">
 
            <chart:SfChart.PrimaryAxis>
                <chart:NumericalAxis />
            </chart:SfChart.PrimaryAxis>
 
            <chart:SfChart.SecondaryAxis>
                <chart:NumericalAxis/>
            </chart:SfChart.SecondaryAxis>
</chart:SfChart> 

C#

public class ViewModel : INotifyPropertyChanged
{
        …….
        private ChartSeriesCollection seriesCollection;
        public ChartSeriesCollection SeriesCollection
        {
            get { return seriesCollection; }
            set
            {
                seriesCollection = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SeriesCollection"));
            }
        }
 
        public ViewModel()
        {
            SeriesCollection = new ChartSeriesCollection();
 
            ColumnSeries columnSeries = new ColumnSeries()
            {
                ItemsSource = ColumnData,
                XBindingPath = "XValue",
                YBindingPath = "YValue"
            };
            SeriesCollection.Add(columnSeries);
 
            LineSeries lineSeries = new LineSeries()
            {
                ItemsSource = LineData,
                XBindingPath = "XValue",
                YBindingPath = "YValue"
            };
            SeriesCollection.Add(lineSeries);
        }        
        ……….   
}

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile