Search code examples
silverlightsilverlight-4.0clone

How to clone Silverlight visual tree structure


I have the same problem as the question stated in "Printing in Silverlight 4".
To get around the problem, I have tried to scale transform root of my visual tree before printing.

    void document_PrintPage(object sender, PrintPageEventArgs e)
    {
        var renderScale = 1.0D;
        if (LayoutRoot.ActualWidth > e.PrintableArea.Width)
            renderScale = e.PrintableArea.Width/LayoutRoot.ActualWidth;

        var scaleTransform = new ScaleTransform();
        scaleTransform.ScaleX *= renderScale;
        scaleTransform.ScaleY *= renderScale;

        e.PageVisual = LayoutRoot;
        e.PageVisual.RenderTransform = scaleTransform;
    }

Now above code correctly prints out with silverlight visuals fit on a piece of paper.

The problem now is that LayoutRoot itself is now scaled down on the screen.
The question is, is there a way for me to create a clone of LayoutRoot before applying scale transform?

My walk-around is to applying the scale tranformation again after printing but I'd like to know if there is a way to clone visual tree


Solution

  • My goodness, thanks for the question. I had the same problem but tried to fiddle about with setting the dimensions of a container (that is already in the visual tree) to the printable area, which does not work, as another layout pass seems to be required. ScaleTransform does work here however instantly.

    I'm fine with the "work around" by just doing a myContainer.ClearValue(FrameworkElement.RenderTransformProperty) in the EndPrint event. Trying to clone the visual tree will yield a plethora of other issues (I have lazy loading content etc).