Data Binding to WCF Service
The purpose of this tutorial is to show you how to populate a RadChart with data from a WCF Service in two ways:
A straight-forward way - plain method calls.
This tutorial will use the Northwind database, which can be downloaded from here.
Before proceeding further with this tutorial you need to create a new application and add a RadChart declaration in your XAML:
The chart control will be populated with the top 10 products from the Northwind database. On the Y axis the UnitPrice property will be displayed.
Add a new SeriesMapping to your chart declaration and set the LegendLabel property to "Products UnitPrice".
Add a new ItemMapping and set the following properties:
FieldName to UnitPrice
DataPointMember to YValue
Creating the WCF Service:
- Add a new item "LINQ to SQL Classes" inside the web server project. Use the .dbml’s designer and drag the *Products *table onto the design surface:
- Then add a new item "Silverlight-enabled WCF Service" to the server project. In the .svc.cs file add the following Linq query to get the first 10 Products from the table:
Now build the project before continuing.
Plain Method Calls
- Add a reference to your WCF Service.
For more information about how to add a reference to a WCF Service and how to create a new instance of a WCF Service client, take a look at the Consuming WCF Service topic.
- Switch to the code-behind and create a new instance of your WCF Service client.
Add the following code in your xaml.cs which will make the initial load of the objects.
Run your demo, the result can be seen on the next image:
Using MVVM Approach
This section will show you how to populate your RadChart control in a MVVM manner.
- Create a new class named NorthwindDataSource.
Add a reference to your WCF Service.
In the NorthwindDataSource class add a reference to an ObservableCollection of Products.
In the NorthwindDataSource class add a reference to your WCF Service client.
For more information about how to add a reference to a WCF Service and how to create a new instance of a WCF Service client, take a look at the Consuming WCF Service topic.
- Add the following code in the constructor of the NorthwindDataSource. It will make the initial load of all Products from the database:
- Declare the NorthwindDataSource object as a resource in your application.
- Update your chart declaration - set the ItemsSource property.
Here it is shown how the final result should look like: