1

I am currently a little bit troubled by the following problem. I have a user interface which basically shows a graphic (a canvas made of Lines, Circles, ... these are all WPF objects). Depending on the selection a user makes in the menu, some items get deleted and some get added. So the basic image looks the same, but a few modifications are made.

The user has the possibility to select - say - 10 different "pages" by clicking a Next/Previous Button.

I am using MVVM Light and my ViewModel contains all the items of the graphic (all Lines, ...).

Now I would like to print that graphic to multiple pages. The first page should contain the graphic with changes from page 1, the second page contains the graphic with changes from page 2 and so on. The actual number of pages is dynamic. I track this with a property CurrentPage and a property PagesTotal. Whenever I push the "Next" button, this causes a command to be executed which will change the variable CurrentPage and also makes sure that the correct items are displayed.

Now I would like to print this but this is where I'm stuck. I dont' mind leaving the MVVM zone and doing some dirty work in code-behind but I would refuse to draw everything again like in the old GDI days.

Any ideas are really welcome.

Tom L.
  • 932
  • 2
  • 9
  • 30

3 Answers3

2

Create a UserControl containing your display logic (you graphic, for instance). Grab you ViewModel list and project then in UserControls, setting each ViewModel as each UserControl's DataContext. Force each one to render calling Measure with infinite value and then Arrange with the resulting DesiredHeight and Width. Then follow the procedures to print WPF visuals (link link link).

Community
  • 1
  • 1
Arthur Nunes
  • 6,718
  • 7
  • 33
  • 46
  • Actually I do have only a single view model and I would like not to make an instance for each page as these can become quite large (10k+ lines). – Tom L. Mar 01 '13 at 21:05
  • With the information you gave it is difficult to guess how your printing system currently works. Can't you share some example code? – Arthur Nunes Mar 04 '13 at 12:49
1

Essentially, this should be quite simple if and only if your views work independently; i.e. your ViewModel doesn't contain UiElements that are placed into your View.

Simple solution is to basically print your visual root. If need be encapsulate your Views in a user control first.

PrintDialog printDlg = new PrintDialog();
UserControl1 uc = new UserControl1();
printDlg.PrintVisual(uc, "User Control Printing."); 

Reference

Meirion Hughes
  • 24,994
  • 12
  • 71
  • 122
  • I separated my ViewModel Objects from the UIElements. The UI Element gets rendered based on a DataTemplate for a normal List<...>. Doesn't the code you gave just print a single UserControl? Because I would like to print the very same user control multiple times (such a control typically consists of ~10000 lines whereas only 100 of them change for each "page"). For the sake of speed I would like to only leave the 9900 elements as they are and exchange the remaining 100. – Tom L. Mar 01 '13 at 21:03
  • You can encapsulate whatever you like inside a parent control. Printing is bit tricky but basically the printing works like you are rendering to the screen, which means you cannot have the same control __instance__ used twice in the logical tree. Whatever you currently have rendering to the screen, try the print with .Print(myWindow). The idea then is rather than have everything in a window, you have everything in a Tall UserControl instead. – Meirion Hughes Mar 01 '13 at 21:10
-1

Alright, I have to admin that I now switched back to doing the printing through code only. I would have really liked doing it "WPF-style" but handling the multiple pages issue was just too much trouble.

Anyway, there is still one issue regarding the printout left but this will be another question.

Tom L.
  • 932
  • 2
  • 9
  • 30