Access Denied
Access Denied

Reputation: 9461

Measuring and displaying are different in UWP

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).

Upvotes: 0

Views: 680

Answers (1)

Fangfang Wu - MSFT
Fangfang Wu - MSFT

Reputation: 1072

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);

Upvotes: 1

Related Questions