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(objec开发者_运维百科t 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.
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
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).
Check out this link for details on silverlight object clone.
also just another idea would using xamlreader/writer to read the xaml string and creating an in-memory copy of the visual tree work.
for ex
If your xaml has button called originalbutton, using the code below you will have a copy of the button in readerLoadButton
// Save the Button to a string.
string savedButton = XamlWriter.Save(originalButton);
// Load the button
StringReader stringReader = new StringReader(savedButton);
XmlReader xmlReader = XmlReader.Create(stringReader);
Button readerLoadButton = (Button)XamlReader.Load(xmlReader);
精彩评论