Converting points from one canvas to another with CreatePageVisual, and TransformToVisual
I have this bit of code that uses DocumentToolkit to Print a XPS document:
protected override UIElement CreatePageVisual(FixedPage fixedPage, Size maxSize) { Canvas page = (Canvas)base.CreatePageVisual(fixedPage, maxSize);
foreach (Tuple<int, UIElement, int, int> t in AllFields.Where(f => f.Item1 == fixedPage.PageNumber))
{
var clone = t.Item2.Clone();
Canvas.SetLeft(clone, t.Item3 * 1.335);
Canvas.SetTop(clone, (790 - t.Item4) * 1.335);
page.Children.Add(clone);
}
}
t.Item2 and t.Item3 are the X,Y coordinates of UIElements on a Canvas. It displays perfectly with these coordinates, but if I go to print the document using the override above my UIElements are "stretched" over the canvas. In the top left they are very close to being correct, but if I go down the page or across the page they are incorrect.
I tried to convert the coordinates using TransformVisual but no luck:
GeneralTransform generalTransform = t.Item2.TransformToVisual(page);
Point childToParentC开发者_如何学编程oordinates = generalTransform.Transform(new Point(t.Item3 * 1.335, (790 - t.Item4) * 1.335));
Canvas.SetLeft(clone, childToParentCoordinates.X);
Canvas.SetTop(clone, childToParentCoordinates.Y);
I get an error, and I'm not sure if it will work. Any suggestions?
It turns out that I had a couple of things wrong. First my TransformToVisual(page) should have been TransformToVisual(page.Parent). And ultimately the real issue that I was having is that I should've been adding my clone's to fixedPage instead of page.
Essentially I was adding my textblocks to a canvas inside of a canvas. I tried to TransformToVisual to fix it, but I ended up deleting all of that and adding my items to fixedPage instead.
精彩评论