WPF Reflections:

Windows Computing

May 14 2008   9:04AM GMT

WPF Problems printing documents



Posted by: Mark Shurmer
WPF, XAML, printing, Windows Computing

Do you have problems printing documents in WPF?

My problem manifested itself when I tried to print a document from a FlowDocumentPageViewer (though the same is true from a FlowDocumentReader).
The problem I was getting was that if I had multiple pages, they would all be scaled and fitted onto the same size of printed page as on the screen.

How do you get around this?
Well the problem lies in the fact that the document is being shown on the screen which has a different size to the printed page.
Realising that, the solution itself is simple - change the height and width of the document before printing it, like:

Assuming docRdr is a FlowDocumentPageViewer and prtDlg is a PrintDialog:

FlowDocument doc = docRdr.Document;
doc.PageHeight  = prtDlg.PrintableAreaHeight;
doc.PageWidth   = prtDlg.PrintableAreaWidth;
prtDlg.PrintDocument(((IDocumentPaginatorSource)doc).DocumentPaginator, “Statement”);

Of course, you will need to remember to save the FlowDocument’s height and width, then re-apply after you have printed the document.

Mar 31 2008   11:01PM GMT

Data validation in WPF using ValidationRule



Posted by: Mark Shurmer
Validation, Databinding, WPF, XAML, Microsoft Windows, Windows Computing

As I mentioned in a previous post, you can specify validation by creating a Validation class (or classes).

You do this by deriving a new class from the ValidationRule class and overriding the Validate method.

Why would you do this, instead of implementing IDataErrorInfo?
Well, if you wanted to do validation across a number of objects, then this is the way to go.
It also provides a level of separation from your business objects, leaving them to deal with business data and the validation objects to deal with validation.
A downside is that it is not backwardly compatible with WinForms :-(

How do you tell your xaml to use it? By specifying it in the ValidationRules element of your binding - just like IDataErrorInfo.


Jan 16 2008   11:59PM GMT

Popup box



Posted by: Mark Shurmer
Development, C, WPF, Windows Computing

One thing that I have noticed, out there in the real world, is that people new to WPF haven’t noticed the (slightly) unsung hero Popup.
I have actually seen people spending man weeks recreating a floating window as per the popup control in WPF.

So what is it? Well it is the ancestor class for menus, tooltips and the combobox - and provides the necessary floating behaviour.

An example of using it is:

<StackPanel>
<ListBox Name=”xxx” />
<Popup PopupAnimation=”Fade” Placement=”Mouse” x:Name=”pop”>
<TextBlock Text=”ggggg” />
</Popup>
</StackPanel>

This example will show a floating window at the mouse cursor, when the mouse is in the stackpanel. Easy as pie isn’t it?