Printing multiple UIElements in Silverlight 4
I have a page that has two UIElement
s that need to be printed, one is a StackPanel
and the other is a custom graph control for displaying test scores. The graph control could be any length based on the number of tests to display, so sometimes I will be able to fit both on one page and other times will need separate pages.
Printing them on separate pages works fine as I just set the UIElement to the pagevisual, the problem I am having is that I can't figure out how to combine them for printing on a single page. I tried creating a StackPanel in the codebehind and adding the elements to it, but since an element can only have one parent I have to create temporary objects to hold each one while I remove from the original parent and then give the 开发者_如何学运维temp to the new StackPanel. The problem is that after I do that all the bound data goes missing
Any ideas would be awesome! Thanks.
Instead of adding actual elements to the StackPanel
for printing, you can create snapshots of those elements and add only images to the StackPanel
. You can use the following method to created an Image
from a UIElement
:
public static Image CreateElementImage(UIElement element)
{
var bitmap = new WriteableBitmap((int)element.RenderSize.Width, (int)element.RenderSize.Height);
Array.Clear(bitmap.Pixels, 0, bitmap.Pixels.Length);
bitmap.Render(element, element.RenderTransform);
bitmap.Invalidate();
var result = new Image {Source = bitmap};
return result;
}
Or just put the image in a Canvas
and then compare Canvas
's height and image height to calculate number of pages.
精彩评论