New to Telerik UI for WPF? Download free 30-day trial

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:

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:

<telerik:RadChart x:Name="radChart" Margin="8" /> 

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

<telerik:RadChart x:Name="radChart" Margin="8"> 
    <telerik:RadChart.SeriesMappings> 
        <telerik:SeriesMapping LegendLabel="Products UnitPrice"> 
            <telerik:SeriesMapping.ItemMappings> 
                <telerik:ItemMapping FieldName="UnitPrice" DataPointMember="YValue"/> 
            </telerik:SeriesMapping.ItemMappings> 
        </telerik:SeriesMapping> 
    </telerik:RadChart.SeriesMappings> 
</telerik:RadChart> 

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:

WPF RadChart

  • 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:

[ServiceContract(Namespace = "")] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class Service2 
{ 
    [OperationContract] 
    public List<Products> LoadTop10Products() 
    { 
        DataClasses1DataContext db = new DataClasses1DataContext(); 
        var query = from c in db.Products select c; 
        return query.Take(10).ToList(); 
    } 
} 

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.

MyService.Service2Client client = new MyService.Service2Client(); 

Add the following code in your xaml.cs which will make the initial load of the objects.

public void SetupService() 
{ 
    InitializeComponent(); 
    MyService.Service2Client client = new MyService.Service2Client(); 
    client.LoadTop10ProductsCompleted += new EventHandler<LoadTop10ProductsCompletedEventArgs>(client_LoadTop10ProductsCompleted); 
    client.LoadTop10ProductsAsync(); 
} 
void client_LoadTop10ProductsCompleted(object sender, LoadTop10ProductsCompletedEventArgs e) 
{ 
    var products = e.Result; 
    this.radChart.ItemsSource = products; 
    this.radChart.DefaultView.ChartArea.AxisX.LabelRotationAngle = -90; 
} 

radChart.ItemsSource = serviceClient.LoadTop10Products(); 

Run your demo, the result can be seen on the next image:

WPF RadChart

Using MVVM Approach

This section will show you how to populate your RadChart control in a MVVM manner.

  • Create a new class named NorthwindDataSource.

public class 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.

public class NorthwindDataSource 
{ 
    private SampleWcfServiceClient serviceClient; 
    public NorthwindDataSource() 
    { 
        this.serviceClient = new SampleWcfServiceClient(); 
        this.Products = new ObservableCollection<Products>(); 
    } 
    public ObservableCollection<Products> Products 
    { 
        get; 
        set; 
    } 
} 

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:

this.serviceClient.LoadTop10ProductsCompleted += new EventHandler<LoadTop10ProductsCompletedEventArgs>(serviceClient_LoadTop10ProductsCompleted); 
this.serviceClient.LoadTop10ProductsAsync(); 

foreach ( Products p in serviceClient.LoadTop10Products() ) 
{ 
    this.Products.Add( p ); 
} 

private void serviceClient_LoadTop10ProductsCompleted(object sender, LoadTop10ProductsCompletedEventArgs e) 
{ 
    foreach (Products p in e.Result) 
    { 
        this.Products.Add(p); 
    } 
} 
  • Declare the NorthwindDataSource object as a resource in your application.

<UserControl.Resources> 
    <example:NorthwindDataSource x:Key="DataSource"/> 
</UserControl.Resources> 
  • Update your chart declaration - set the ItemsSource property.

<telerik:RadChart x:Name="radChart" Margin="8"     
                  ItemsSource="{Binding Source={StaticResource DataSource}, Path=Products}"> 
    <telerik:RadChart.SeriesMappings> 
        <telerik:SeriesMapping LegendLabel="Products UnitPrice"> 
            <telerik:SeriesMapping.ItemMappings> 
                <telerik:ItemMapping FieldName="UnitPrice" DataPointMember="YValue"/> 
            </telerik:SeriesMapping.ItemMappings> 
        </telerik:SeriesMapping> 
    </telerik:RadChart.SeriesMappings> 
</telerik:RadChart> 

Here it is shown how the final result should look like:

WPF RadChart