Articles in this section
Category / Section

How to bind series property using MVVM pattern in WPF Chart?

2 mins read

In WPF Chart, 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

Conclusion

I hope you enjoyed learning how to bind series property using MVVM pattern in WPF Chart.

You can refer to our WPF Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF Chart example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied