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 the series collection property using MVVM pattern?

Platform: WPF |
Control: SfChart |
Published Date: September 28, 2018 |
Last Revised Date: June 27, 2019

You can bind the Series property using MVVM pattern in which the ChartSeriesCollection class has been used. Please refer to the following code sample for binding the Series property.

Xaml:

 

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

       

        <StackPanel.DataContext>

            <local:ViewModel x:Name="viewmodel"/>

        </StackPanel.DataContext>

       

        <Button x:Name="AddSeries" Content="AddSeries" Width="700" Height="40" Click="AddSeries_Click" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Transparent" />

        <Button x:Name="RemoveSeries" Content="RemoveSeries" Width="700" Height="40" Click="RemoveSeries_Click" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Transparent" />

 

        <chart:SfChart x:Name="Chart" Series="{Binding Series}" >

            <chart:SfChart.Legend>

                <chart:ChartLegend />

            </chart:SfChart.Legend>

            <chart:SfChart.PrimaryAxis>

                <chart:CategoryAxis />

            </chart:SfChart.PrimaryAxis>

            <chart:SfChart.SecondaryAxis>

                <chart:NumericalAxis/>

            </chart:SfChart.SecondaryAxis>

        </chart:SfChart>

    </StackPanel>

   

C#:

 

private void AddSeries_Click(object sender, RoutedEventArgs e)

{

           viewmodel.Series.Add(new LineSeries()

           {

            ItemsSource = viewmodel.LineData4,

            XBindingPath = "XValue",

            YBindingPath = "YValue",

            Label = "Series4",

            AdornmentsInfo = new ChartAdornmentInfo() { ShowMarker = true, SymbolInterior = new SolidColorBrush(Colors.Black), Symbol = ChartSymbol.Diamond } });

 }

 

private void RemoveSeries_Click(object sender, RoutedEventArgs e)

{

    viewmodel.Series.RemoveAt(0);

}

 

public class ViewModel : INotifyPropertyChanged

{

        private ChartSeriesCollection series;

 

        public event PropertyChangedEventHandler PropertyChanged;

 

        public ChartSeriesCollection Series

        {

            get { return series; }

            set

            {

                series = value;

                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Series"));

            }

        }

 

        public ObservableCollection<Model> LineData1 { get; set; }

 

        public ObservableCollection<Model> LineData2 { get; set; }

 

        public ObservableCollection<Model> LineData3 { get; set; }

 

        public ViewModel()

        {

            LineData1 = new ObservableCollection<Model>()

            {

                new Model("Benz", 10),

                new Model("Volvo", 20),

                new Model("Jaguar", 30),

                new Model("BMW", 35),

                new Model("Mahendra", 30),

                new Model("Toyoto", 20),

                new Model("Audi", 10),

            };

 

            LineData2 = new ObservableCollection<Model>()

            {

                new Model("Benz", 20),

                new Model("Volvo", 30),

                new Model("Jaguar", 40),

                new Model("BMW", 45),

                new Model("Mahendra", 40),

                new Model("Toyoto", 30),

                new Model("Audi", 20),

            };

 

            LineData3 = new ObservableCollection<Model>()

            {

                new Model("Benz", 30),

                new Model("Volvo", 40),

                new Model("Jaguar", 50),

                new Model("BMW", 55),

                new Model("Mahendra", 50),

                new Model("Toyoto", 40),

                new Model("Audi", 30),

            };

 

            Series = new ChartSeriesCollection();

            Series.Add(new LineSeries()

            {

                ItemsSource = LineData1,

                XBindingPath = "XValue",

                YBindingPath = "YValue",

                Label = "Series1",

                AdornmentsInfo = new ChartAdornmentInfo() { ShowMarker = true,SymbolInterior= new SolidColorBrush(Colors.Black), Symbol= ChartSymbol.Diamond }

            });

 

            Series.Add(new LineSeries()

            {

                ItemsSource = LineData2,

                XBindingPath = "XValue",

                YBindingPath = "YValue",

                Label = "Series2",

                AdornmentsInfo = new ChartAdornmentInfo() { ShowMarker = true,SymbolInterior= new SolidColorBrush(Colors.Black), Symbol= ChartSymbol.Diamond }

            });

 

            Series.Add(new LineSeries()

            {

                ItemsSource = LineData3,

                XBindingPath = "XValue",

                YBindingPath = "YValue",

                Label = "Series3",

                AdornmentsInfo = new ChartAdornmentInfo() { ShowMarker = true, SymbolInterior = new SolidColorBrush(Colors.Black), Symbol = ChartSymbol.Diamond }

            });

 

        }

}

 

public class Model

{

        public string XValue { get; set; }

 

        public double YValue { get; set; }

 

        public Model(string xvalue, double yValue)

        {

            XValue = xvalue;

            YValue = yValue;

        }

}

Output:

Chart rendered by binding the series collection property using MVVM pattern in UWP

2X faster development

The ultimate WPF 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