Monday, July 13, 2009

Infragistics WPF Reporting

I’ve been looking for flexible Reporting components for the WPF application I’m working on, and strangely enough, there are just a few. I think WPF component in general is still a work in progress, and even if you have the chance to find a working component, there ALL have a lot of short comings and lack design-mode features, developer-friendliness, etc. and when it comes down to reporting component, there is Infragistics with very basic feature set, ComponentOne with years of background in reporting tools and Stimulsoft with complete reporting set for WinForm, WPF and Web.

So, I start evaluating each one. Stimusoft WPF reporting looked very promising with all bells and whistles like end-user WPF designer and the whole UI (designer and preview) were in WPF, but If I’m going to buy a component I need more that just a reporting tool, how about having bunch of other controls as well? I skipped to ComponentOne and after downloading their package (around 100 MB download), the damn installation did not work and I was staring at the installation error message that had to do something with packaging. So in reality, I had only one more choice : Infragistics.

Infragistics Reporting

With very basic feature, Infragistics reporting is also easy to use. Since there is no designer (yet?) you have to code your report and create various report sections (Header, Footer, etc.) and add related stuff into that sections, all in code. The great thing about this reporting control (also a feature of ComponentOne) is that you can print any kind of UIElement, this means you can print your whole UserControl or Form which comes in handy.

Infragistics-Reporting

The real power of Infragistics reporting is when combined with their “Data Presenter” or “XamDataGrid” control. End-user can apply filtering, grouping, sorting, etc. and just print the result, easy as that.

RightToLeft Issue

When evaluating my application which is a multi-lingual application that supports RightToLeft and LeftToRight reading ordered languages, I came across a problem when printing controls with RightToLeft flow direction. The problem was that the whole control is “Mirrored”. Not that this mirroring is an issue, but all the texts and values printed are also mirrored which makes the whole report useless.

Mirrorred Report

Workaround

The workaround is, of course, not to print the control in RightToLeft and use LeftToRight for the time being until the bug is fixed. As easy as this sounds, it is not very easy to do for some reasons:

  • RightToLeft is usually set on the parent (Form), and controls just “inherit” the setting from their parent.
  • If you do change parent’s RightToLeft end-user will see the effect.

But you can always clone the object and change the behavior, right? But you can not do so with Visual Elements. There’s no Copy / Clone method on Visual elements in WPF, so how do we “clone” the control, change the FlowDirection, and hand it to the reporting?

Fortunately, WPF comes with two neglected classes : “XamlWriter” and “XamlReader” each of which do as exactly as the name puts it. Using “XamlWriter” you can write a Visual object (UIElement) into a string, then use “XamlReader” ton construct it back. This is just about the UI, so you’ll have to re-attach the “DataSource” and “DataContext” you might have. Here’s the snippet to do the trick:

public XamDataGrid Clone(XamDataGrid printable)
{
//Clone the grid control
var o = XamlWriter.Save(printable);
var sr = new StringReader(o);
var reader = XmlReader.Create(sr, new XmlReaderSettings());
var result = (XamDataGrid)XamlReader.Load(reader);

//It's a clone, need to reassign data source
result.DataSource = printable.DataSource;

//Set flow direction to LTR
result.FlowDirection = FlowDirection.LeftToRight;

return result;
}
You can download the whole sample that shows the issue and the workaround. To compile and run it you need to have Infragistics 2009.1 installed. You can download Infragistics package here.
Submit this story to DotNetKicks Shout it

2 comments:

Anonymous said...

why aren't you using C1Report?
im using it in my wpf applications and it is very good.
if you have any proplem in installing c1.wpf, try to install c1.studio.

Hadi Eskandari said...

Mostly because it was a very large data. I had previous experience with C1 Report (on WinForm) and it was a good reporting tool IMHO.

I'll try downloading the C1 Studio to see if I can install it.