Before starting, I presume that you have some knowledge of WPF and XAML.
Getting Started: MVVM Pattern Using Prism Library in WPF
Before starting, I hope you have added ‘Prism.Unity’ from ‘NuGet Packages’ and for charting, we will do the same using the charting 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 the project and select "Manage NuGet Packages".

Step 3
- For charting controls, type ‘System.Windows.Controls.DataVisualization ‘ in browse section, select that package, and click "Install" button.

Afterwards, the package is installed successfully.

Now, you can see the newly added DLLs 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.
- Create a View named ‘TestView.xaml’ in Views folder.
- Create a Model named ‘TestModel.xaml’ in Models folder.
- Create a ViewModel named ‘TestViewModel.xaml’ in ViewModels folder.



Step 6
Add the namespace named Prism.MVVM in the TestModel page to inherit the class named ‘Bindable Base’.
- Create a class for chart binding with the key and the value properties for Axis-X and Y.
- class Keyvalue : BindableBase
- {
- private string _Key;
- public string Key { get { return _Key; } set { SetProperty(ref _Key, value); } }
- private int _Value;
- public int Value { get { return _Value; } set { SetProperty(ref _Value, value); } }
- }
- Create a list type property named DataList to store multiple values of keyvalue class.
- class TestModel : BindableBase
- {
- private List<Keyvalue> _DataList;
- public List<Keyvalue> DataList { get { return _DataList; } set { SetProperty(ref _DataList, value); } }
- }

Step 7
On the TetsView page, we do the following.
- Access the assembly of charting on the top of the View.
- xmlns:ChartToolKit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
- Create 2 group boxes with different header names for static and dynamic charts respectively, where the first will contain a chart with a column series with an itemsource, IndependentvaluePath and DependentvaluePath property.
Note - Here, an Itemsource property is bound with ‘DataList’ property, which we have created in TestModel class and for IndependentvaluePath & DependentvaluePath property, we use ‘Key’ and ‘Value’ of KeyValue class. - The 2nd groupbox didn’t contain anything; as it just has a name.
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition></RowDefinition>
- <RowDefinition></RowDefinition>
- </Grid.RowDefinitions>
- <GroupBox Grid.Row="0" Header="Static Column Chart">
- <ChartToolKit:Chart>
- <ChartToolKit:ColumnSeries ItemsSource="{Binding Path=TestModel.DataList,
- UpdateSourceTrigger=PropertyChanged,Mode=TwoWay, NotifyOnSourceUpdated=True }"
- IndependentValuePath="Key"
- DependentValuePath="Value">
- </ChartToolKit:ColumnSeries>
- </ChartToolKit:Chart>
- </GroupBox>
- <GroupBox Grid.Row="1" Header="Dynamic Column Chart" x:Name="GroupBoxDynamicChart">
- </GroupBox>
- </Grid>

Step 8
Add PrismMVVMTestProject.ViewModels namespace and bind Data Context of TestView page to the ViewModel named ‘TestViewModel’. and pass the second groupbox into it.
- using System.Windows;
- using PrismMVVMTestProject.ViewModels;
- namespace PrismMVVMTestProject.Views
- {
- /// <summary>
- /// Interaction logic for TestView.xaml
- /// </summary>
- public partial class TestView : Window
- {
- public TestView()
- {
- InitializeComponent();
- this.DataContext = new TestViewModel(GroupBoxDynamicChart);
- }
- }
- }

Step 9
- Add the namespace named ‘Prism.MVVM’ and ‘PrismMVVMTestProject.Models’ in the TestViewModel page to inherit the class named ‘Bindable Base’ and access TestModel in this page.
- Create a property of TestModel class object where ‘ref‘ parameter allows you to update its value.
- Create a local list of Keyvalue class named ‘tempList’ and add some data into it.
- List<Keyvalue> tempList = new List<Keyvalue>();
- tempList.Add(new Keyvalue() { Key = "Rs.100", Value = 50 });
- tempList.Add(new Keyvalue() { Key = "Rs.500", Value = 18 });
- tempList.Add(new Keyvalue() { Key = "Rs.1000", Value = 25 });
- tempList.Add(new Keyvalue() { Key = "Rs.2000", Value = 5 });
- Assign ‘tempList’ to ‘TestModel.DataList’ to bind the static chart.
- //Bind the sttaic Chart
- TestModel.DataList = tempList;
- Create the dynamic chart and column series.
- Bind the item source of column series with ‘tempList’.
- To bind the IndependentvaluePath & DependentvaluePath property, we use ‘Key’ and ‘Value’ of KeyValue Class same as in static chart.
- Add the series in to the chart and chart into the groupbox.
- //Create the Dynamic Chart, Bind it and add it into the 2nd GroupBox
- Chart dynamicChart = new Chart();
- ColumnSeries columnseries = new ColumnSeries();
- columnseries.ItemsSource = tempList;
- columnseries.DependentValuePath = "Value";
- columnseries.IndependentValuePath = "Key";
- dynamicChart.Series.Add(columnseries);
- GroupBoxDynamicChart.Content = dynamicChart;















pooja singhPosted Oct 30, 2019, 5:47 AM
How to reduce the size of bars in this project? i'm finding difficulty to achieve this.
Quốc Toản NgôPosted Mar 20, 2018, 11:14 AM
Great! thanks a lot