I’m developing a program to do sailing race results for my club. After some deliberation I elected to use WPF 4 as it finally includes the DataGrid control which I’ve always thought was essential. I’ll blog about things I’ve done to make editing and entering results as easy as possible soon as that was an interesting learning curve and what at first appeared to be quite limited is finally working well.
With regards to printing WPF provides some very interesting and very powerful features and overall I’m impressed at how easy it is to produce printed output, though there are a few oddities.
These mostly came to light as I started working on printing race entry sheets which are set out before the race for competitors to enter with a set of boxes for the Race Officer to enter lap finish times. Essentially what I wanted was a single page containing a grid with equal size columns laid out landscape.
First I set up a Page with a DataGrid and fixed columns giving each column a star width. I added 15 empty rows to the grid to get a table. Getting a Page into a FixedDocument (which I will also detail elsewhere), is a bit of a palaver but I’d already worked out how to do that and so I got that done and that for Landscape I needed to change the PageOrientation on FixedDocument’s DocumentPaginator.
First problem, did a preview using a DocumentViewer and things look good, but print and it comes out Portrait mode on the paper and half my grid is missing, and after much searching I found the following:
I’m already using a custom template for a DocumentViewer to remove the Search box, and the template I’d got from the standard control doesn’t have a print button and so there is no way to intercept the print command that I could see.
Two issues with DocumentViewer in my opinion:
1. Need to be able to turn off the search facility
2. Need simple way to specify print orientation or it should work it out for itself (I’m passing a FixedDocument after all which knows the orientation!).
So I looked at printing without previewing.
The next issue which caused much head scratching, the DataGrid columns weren’t sized equally when printed but instead were given a minimal size despite the grid stretching across the page. I tried calling Measure and Arrange on the Page and on the DataGrid but neither helped. Eventually I discovered I had to call BeginInit and EndInit after Measure and Arrange to do the sizing of the columns.
The dynamic DataGrid column sizing didn’t happen because I wasn’t previewing, i.e. making the document visible.
What I evenually also realised was that I didn’t need the Page at all or the FixedDocument, I could create a UserControl and set it’s size to the page, Measure, Arrange, BeginInit and EndInit and then print directly as a Visual.
