When the report definition files using Custom Fields (like Charts) is used in C1WpfReport, the following exception is observed:
InvalidCastException (Failed to create custom field 'C1.C1Report.CustomFields.Chart' from assembly 'c1.c1report.customfields.4')

This issue is observed as the CustomFields dll and the report definition file is generated with a specific version of C1WpfReport. This blog is to help you to resolve the error. Here are few specific steps that you need to take to make this work. Let me elaborate them in detail.

Step 1 : Regenerate C1.WPF.C1Report.CustomFields.dll


Open the WPF CustomFields sample in "....Documents\ComponentOne Samples\Studio for WPF\C1.WPF.Report\CS\CustomFields". This sample is built on .NET framework 3.0, so change its Target framework to 4.0. After this remove all the C1 references from the

project. Also, remove the PresentatioCore.dll and add PresentationCore.dll compatible with .NET framework 4.0. You would find that under.NET tab in the "Add References.." window.

Add following .NET framework 4.0 compatible C1 dll's available in "C:\Program Files\ComponentOne\Studio for WPF\C1WPFReport\Designer.4" folder :

  • C1.C1Report.4.Design

  • C1.C1Report.CustomFields.4

  • C1.Win.C1Chart.4

  • C1.WPF.C1Report.4


Modify the licenses file and replace the contents with the following line :
C1.C1Report.C1Report, C1.WPF.C1Report.4

Rebuild this sample in order to generate C1.WPF.C1Report.CustomFields.dll. Now add the generated dll to your project.

Step 2 : Add references to your application


Open your application and add reference to the following dlls in your project :

  • C1.WPF.C1Report.4.dll (from "C:\Program Files\ComponentOne\Studio for WPF\bin\v4" folder")

  • C1.WPF.C1Report.CustomFields.dll (created in Step 1)

  • C1.Win.C1Chart.4.dll (from "C:\Program Files\ComponentOne\Studio for WPF\C1WPFReport\Designer.4" folder)


Step 3 : Modify report definition file


The xml report definitions that you are loading on the should be created with the same designer version. If not, you can change edit the xml definition. Generally,xml report definitions start with following text:-
<?xml version="1.0"?>
<Reports>
<!--Report *** Products Report ***-->
<Report version="4.6.20112.54408">       THIS VERSION SHOULD BE SAME AS THE VERSION OF C1REPORT DLL IN YOUR PROJECT
.
.
.
..


You also need to edit the xml report definition with the assembly to be used in rendering the custom fields. So if you open up any report definition, you would notice following text in field definition:-

<Field>
<Name>chartField</Name>
<Section>5</Section>
<Left>540</Left>
<Top>180</Top>
<Width>6660</Width>
<Height>4680</Height>
<PictureScale>1</PictureScale>
<CustomField asm="C1.C1Report.CustomFields.4">  THIS SHOULD BE EDITED AND REPLACED WITH WPF CUSTOMFIELDS REFERENCE..
<DataX>ProductName</DataX>
<DataY>[Unit Price];[Reorder Level]</DataY>
<DataColor>8388608</DataColor>
</CustomField>
</Field>


So, the modified xml text would be:-

<Field>
<Name>chartField</Name>
<Section>5</Section>
<Left>540</Left>
<Top>180</Top>
<Width>6660</Width>
<Height>4680</Height>
<PictureScale>1</PictureScale>
<CustomField asm="C1.WPF.C1Report.CustomFields">
<DataX>ProductName</DataX>
<DataY>[Unit Price];[Reorder Level]</DataY>
<DataColor>8388608</DataColor>
</CustomField>
</Field>



Now you can load the report definition files in your WPF application without any problem. I am attaching a sample implementing the above steps with C1WpfReport build 4.6.20112.54408.

Download Sample