WPF Reflections:

May, 2008

May 25 2008   10:03PM GMT

WPF DataTriggers with enums



Posted by: Mark Shurmer
WPF

This is something that I had to do recently, and it wasn’t as obvious as I thought.

Given a very simple enum like:

blog25may2008.JPG

The first (and obvious) try is:

blog-25may2008-3.JPG

Here we are trying to hide a label and a textbox based on the value of the enum.
However, it doesn’t work :-(

As an aside, the autogrid you see heer is very handy, and can be found on www.codeplex.com

What you actually need to do is slightly odd, and is effectively instantiating the enum values:

blog-25may2008-4.JPG

May 22 2008   11:33AM GMT

WPF Controls - how to avoid designer nastiness



Posted by: Mark Shurmer
WPF, Controls, XAML

Have you ever written any user controls or custom controls?
The difference between them is that with a custom control you have full scope to render it (plus you have to do it), whereas a user control is a container to amalgamate other controls.

With both of them though, you need to make sure that any calls made out of them are minimised.

Why?

Well because when they are created in WPF designers like Visual Studio 2008 or Expression Blend, they will be instantiated and therefore constructors will be called.
If you have calls out to databases etc, you will get a nasty looking exception thrown, or just the Visual Studio 2008 problem loading display:

blog-22may2008.JPG

How do you sort this problem?

By using the GetIsInDesignMode static method of DesignerProperties, like so
if (!DesignerProperties.GetIsInDesignMode(App.Current.MainWindow))
// do something you don’t want to appear in designer


May 16 2008   4:52PM GMT

WPF Controls



Posted by: Mark Shurmer
WPF, Controls

As I am architecting a new Line Of Business system for a customer, one of my thoughts is to recheck the situation with WPF controls.

The power and styleability of WP, with a few small exceptions, means that there isn’t quite the need for third party control libraries as there was in WinForms. In the days of WinForms it was a question of which of the main control libraries you picked, and how that was achieved with whatever ridiculous purchasing system was in place.

Now with WPF (and Silverlight soon), the focus has changed. There are a couple of holes in the WPF offering, like a paging grid, date time picker, numeric value editor. However, if you look at codeplex, most of the holes have been addressed by free controls. Also Xceed moved the goalposts for the component writing companies by giving away a top quality grid.
However, it’s not beyond some development teams to write their own controls as well - whereas that would have been a long, frustrating and expensive process in Winforms. I know because I have seen it :-(
Another bad effect I see, is that some companies just won’t bother - and try to muddle through on their own , which does seem a mistake to me.

What does that mean for the major component vendors?
Well, in my opinion, it means they need to up their gameplan, and provide components on a different level. They can’t just provide decent grids and toolbars etc, that were missing from Visual Studio.
To be fair we have started to see the beginning of that, with carousels and ribbons


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.


May 14 2008   8:50AM GMT

WPF Printing documents



Posted by: Mark Shurmer
WPF, printing, XAML

I have blogged before on how you print a WPF element using the PrintVisual method in the PrintDialog class, which is very flexible as it will print any element derived from Systems.Windows.Media.Visual.
However, there is another weapon in the PrintDialog armoury, and that is the ability to print documents. Given the extensive document support in WPF, it comes as no surprise that you can easily print these documents.

To do that you need to get at the DocumentPaginator object - it’s purpose in life lies in managing content as pages which is just perfect for the job.
You get at it by using the IDocumentPaginatorSource interface, like this:

assuming docRdr is a FlowDocumentScrollViewer

PrintDialog prtDlg = new PrintDialog();
if(prtDlg.ShowDialog() == true)
{

prtDlg.PrintDocument(((IDocumentPaginatorSource)docRdr.Document).DocumentPaginator, “Statement”);

}

This will print the document for you just fine