Static And Dynamic Column Chart In WPF With MVVM Pattern Using Prism Library

I hope you have some knowledge of WPF and XAML before starting.

To know more about MVVM using Prism, follow.

Before starting, I hope you added the ‘Prism.Unity’ from ‘NuGet Packages’ and for the chart, we will do the same for thr chart controls package.

Note

In this article, I am using Visual Studio 2015.

Step 1

Create a project named ‘PrismMVVMTestProject’ of WPF Application.



Step 2

Right click on project and select ‘Manage NuGet Packages’.



Step 3

For chart controls, type ‘System.Windows.Controls.DataVisualization‘ in browse section, select that package and click on install button.



After successfully installing the package, the screenshot is given below..



Now, you can see the newly added DLL's in the reference section of the project.



Step 4

It’s a better approach to create three different folders in the project for Model, View and View model respectively.



Step 5

Create pages in all the folders.

  1. Create a view Named ‘TestView.xaml’ in the Views folder.



  2. Create a model named ‘TestModel.xaml’ in the Models folder.



  3. Create a ViewModel named ‘TestViewModel.xaml’ in the ViewModels folder.


Step 6

Add the namespace named ‘Prism.MVVM’ in the TestModel page to inherit the class named ‘Bindable Base’.

  1. Create a class for chart binding with the key and value properties for axis-X and Y.
    1. classKeyvalue BindableBase {  
    2.     privatestring _Key;  
    3.     publicstring Key {  
    4.         get {  
    5.             return _Key;  
    6.         }  
    7.         set {  
    8.             SetProperty(ref _Key, value);  
    9.         }  
    10.     }  
    11.     privateint _Value;  
    12.     publicint Value {  
    13.         get {  
    14.             return _Value;  
    15.         }  
    16.         set {  
    17.             SetProperty(ref _Value, value);  
    18.         }  
    19.     }  
    20. }  
  2. Create a list type property named DataListto to store multiple values of keyvalue class.
    1. classTestModel BindableBase {  
    2.     privateList < Keyvalue > _DataList;  
    3.     publicList < Keyvalue > DataList {  
    4.         get {  
    5.             return _DataList;  
    6.         }  
    7.         set {  
    8.             SetProperty(ref _DataList, value);  
    9.         }  
    10.     }  
    11. }  

Step 7

On the TetsView page, we do as shown below.

  1. Access the assembly of the chart on the top of the view.
    1. xmlnsChartToolKit="clr-namespaceSystem.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"  
  2. Create two GroupBoxes with different header names for the static and dynamic chart respectively, where the first will contain a chart with a column series with an itemsource ,IndependentvaluePath and DependentvaluePath property.

    Note

    Here, Itemsource property is binded with ‘DataList’ property, which we have created in TestModel class and for IndependentvaluePath&DependentvaluePath property, we use ‘Key’ and ‘Value’ of KeyValue class.

  3. The second groupbox didn’t contain anything, as it just has a name.
    1. <Grid>  
    2.     <Grid.RowDefinitions>  
    3.         <RowDefinition></RowDefinition>  
    4.         <RowDefinition></RowDefinition>  
    5.     </Grid.RowDefinitions>  
    6.     <GroupBoxGrid.Row="0" Header="Static Column Chart">  
    7.         <ChartToolKitChart>  
    8.             <ChartToolKitColumnSeriesItemsSource="{Binding Path=TestModel.DataList,  
    9. UpdateSourceTrigger=PropertyChanged,Mode=TwoWay,NotifyOnSourceUpdated=True }" IndependentValuePath="Key" DependentValuePath="Value">  
    10.                 </ChartToolKitColumnSeries>  
    11.         </ChartToolKitChart>  
    12.         </GroupBox>  
    13.         <GroupBoxGrid.Row="1" Header="Dynamic Column Chart" xName="GroupBoxDynamicChart">  
    14.             </GroupBox>  
    15. </Grid>  

Step 8

Add PrismMVVMTestProject.ViewModels namespace and bind DataContext of TestView page to the ViewModel named ‘TestViewModel’.and pass the 2ndgroupbox into it.

  1. usingSystem.Windows;  
  2. usingPrismMVVMTestProject.ViewModels;  
  3. namespacePrismMVVMTestProject.Views {  
  4.     ///<summary>  
  5.     /// Interaction logic for TestView.xaml  
  6.     ///</summary>  
  7.     publicpartialclassTestView Window {  
  8.         publicTestView() {  
  9.             InitializeComponent();  
  10.             this.DataContext = newTestViewModel(GroupBoxDynamicChart);  
  11.         }  
  12.     }  
  13. }  


Step 9 
  1. Add the namespace named ‘Prism.MVVM’ and ‘PrismMVVMTestProject.Models’ in the TestViewModel page to inherit the class named ‘Bindable Base’ and accessing TestModel in this page.
  2. Create a property of TestModel class object, where ‘ref‘ parameter allows you to update its value.
  3. Create a local list of Keyvalue classnamed ‘tempList’ and add some data into it.
    1. List < Keyvalue > tempList = newList < Keyvalue > ();  
    2. tempList.Add(newKeyvalue() {  
    3.     Key = "Rs.100", Value = 50  
    4. });  
    5. tempList.Add(newKeyvalue() {  
    6.     Key = "Rs.500", Value = 18  
    7. });  
    8. tempList.Add(newKeyvalue() {  
    9.     Key = "Rs.1000", Value = 25  
    10. });  
    11. tempList.Add(newKeyvalue() {  
    12.     Key = "Rs.2000", Value = 5  
    13. });  
  4. Assign ‘tempList’ to ‘TestModel.DataList’ to bind the static chart.
    1. //Bind the sttaic Chart   
    2. TestModel.DataList = tempList;  
  5. Create the dynamic chart and column series.
  6. Bind the item source of the column series with ‘tempList’.
  7. To bind the IndependentvaluePath&DependentvaluePath property, we use ‘Key’ and ‘Value’ of KeyValue class, which is similar as in Static chart.
  8. Add the series in to the chart and chart into the groupbox. 
    1. //Create the Dynamic Chart, Bind it and add it into the 2nd GroupBox  
    2. ChartdynamicChart = newChart();  
    3. ColumnSeriescolumnseries = newColumnSeries();  
    4. columnseries.ItemsSource = tempList;  
    5. columnseries.DependentValuePath = "Value";  
    6. columnseries.IndependentValuePath = "Key";  
    7. dynamicChart.Series.Add(columnseries);  
    8. GroupBoxDynamicChart.Content = dynamicChart;  

Run the page and see the output.

Up Next
    Ebook Download
    View all
    Learn
    View all