How to update a UserControl size which will never come in to view (only be printed)

I create a Usercontrol in code which get's a Datacontext and than get printed but i need to refresh the Usercontrol so it get resized (Stretch to fit the Content) but i don't know how i should do this

my Code

var view = new DruckV();
view.DataContext = this;
// now i need to resize it

i tried

view.UpdateLayout();// width and height will remain by 0,0

than i can call my Printer

var printer = new Printer();
printer.Print(view);

so how should i do this because i can't use

view.Measure(UnknownSize);
view.Arrange(new Rect(new Point(0, 0), UnknownSize));

Answers


If you use 3.5 .net framework you can create a container for the control and perform Measure/Arrange for infinity size (or if control has a dynamic dimensions that depends on container size - set maximum allowed size to the container):

var panel = new StackPanel();
//panel.MaxWidth = 100; //if you control has dynamic sizing
//panel.MaxHeight = 100;
panel.Children.Add(view);
var infinitySize = double.PositiveInfinity,double.PositiveInfinity;
panel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
panel.Arrange(new Rect(0, 0, panel.DesiredSize.Width, panel.DesiredSize.Width));
view.UpdateLayout();

Unfortunately on .net 4.x this solution doesn't worked for me and only way I have found to force update controls size is create new Window with container (same as described above) and show this window outside a display screen (set Window.Left =-1000;)

BTW: in case of rendering by invisible window, size of the rendering control can't be larger than monitors screen size, and to render huge controls you have to perform ScaleTransformon it.


Need Your Help