Search code examples
c#wpfcontrolsmeasure

How to re-Arrange and re-Measure a UserControl when it's removed from its parent?


I have a Border which contains a UserControl for previewing before printing.

Since the calculations done in this UserControl are quite time consuming, i'd like to print the instance that currently resides in the Border.

However, when I remove this UserControl from its parent (the Border), I can't seem to re-Arrange and re-Measure its dimensions.

The UserControl ActualWidth and ActualHeight properties are not affacted by the re-Arrange and re-Measue.

Here's how I do it:

XZReport rap = new XZReport();
xzPreview.Child = rap;

...

xzPreview.Child = null;
PrintDialog dlg = new PrintDialog();

rap.Measure(new Size(dlg.PrintableAreaWidth, double.PositiveInfinity));
rap.Arrange(new Rect(rap.DesiredSize));
rap.UpdateLayout();
// setting a breakpoint here, I see ActualWidth and ActualHeight properties are not changed

Solution

  • Solved it by moving my calculations to a ViewModel, it then allowed me to use the first instance of my ViewModel without re-calculation everything, and allows me to create a new instance of my View without affecting the measurement of the dimensions for printing.