0

I wanted to implement itemsControl printing with page breaks like it's described in the following blog: http://blogs.u2u.be/diederik/post/2013/05/21/Printing-a-XAML-ItemsControl-from-a-Windows-8-Store-app.aspx

The problem is that in Windows 10 it works incorrectly (I added background to show items in itemsControl for debugging): win 10 version

While 8.1 version works as expected: win 8.1 version

Is there any idea what can be wrong? All it does is just adds paragraphs to richtext, measures each item content and then splits it by pages. Windows 10 sample is here. Windows 8.1 sample is here.

UPDATE:

More info: I changed textblock to textbox (wanted to add background to text). It turns out that it works better, but text is cut at the end of the page. It started working on all pages except for the first page in preview (text is single line and not wrapping on the first page for some unknown reasons). It works correctly in printed document. Weird thing is that it works correctly if I close preview and click print again (even though last line on page is still cut).

Community
  • 1
  • 1
Access Denied
  • 8,723
  • 4
  • 42
  • 72

1 Answers1

1

I have tried your sample code: The problem is that the system default templates for your two applications are not all the same, so that there is a little difference in the display mode for the item. To fix the problem in the win10 app, you'd better define a proper layout style for the control. There are various ways to achieve this: adding a proper template in resources, or defining in code.

Please try following code in the "PreparePrintContent" function. Please notice that I adjust the layout by setting margin of the UIElement. This is a very simple approach just for your reference:

var x = itemsControl.ContainerFromItem(item) as ContentPresenter;
Paragraph p = new Paragraph();
InlineUIContainer c = new InlineUIContainer();
var o = x.ContentTemplate.LoadContent() as UIElement;
(o as FrameworkElement).DataContext = item;
(o as FrameworkElement).Margin = new Thickness(40);
Fangfang Wu - MSFT
  • 1,062
  • 6
  • 7
  • Thanks for your efforts, but I still didn't get what is the reason of the problem. 1. Why 40? 2. For which control should I define style? 3. The problem is that measuring and displaying is different. Why measuring and displaying is different for the same XAML? It's OK if windows 10 will look different, the question is where margin between textblocks come from? – Access Denied Nov 12 '15 at 04:29
  • 1. "40" is just a very rough try data. You can adjust it based on your requirement for print preview. 2.In PreparePrintContent(), you load the content as an UIElement or Grid( in your sample), which needs a customized style. 3. Even for a same XAML, the system default settings for some UIElements are not all the same depends on whether it is a win8.1 app or a UWP app. There is lots of way to adjust the style of the UIElement. In my reply I just use a very simple method for example. – Fangfang Wu - MSFT Nov 12 '15 at 05:15
  • Let's assume there is no windows 8.1 version. So, in this sample we load UI element (Grid) and measure it. Then we render it and it turns out that textblock spreads outside it's parent (Grid). What you did is just found some magic number which compensate this spreading error. – Access Denied Nov 12 '15 at 08:08
  • @FangfangWu-MSFT is there a 'fast' way to clear all system settings? So that we don't need to rebase ourselfs? I'm facing the same issue. – Depechie Dec 30 '15 at 17:14