1

In Windows Forms, you have a PreferredSize property that will tell you how large a control would like to be.

Where's that property in WPF?

I have a Grid with some content (of unknown size) and would like to create an animation that increases the grid in height from 0 up to its preferred (auto) height. Of course the grid is either at 0 height or collapsed at the beginning, because it's not supposed to pop up in an instant but smoothly "fade in". So I cannot use the ActualHeight property for the animation target because it is always 0. The opposite direction animation should be easier because I can animate from ActualHeight (or just no explicit start value) to 0.

ygoe
  • 18,655
  • 23
  • 113
  • 210
  • Meanwhile, I have tried all sorts of DesiredSize, UpdateLayout, InvalidateMeasure, ActualHeight and such, but at best it only works a single time and then the height stays 0 again. MSDN and IntelliSense didn't help me out. – ygoe Mar 16 '12 at 10:31
  • Perhaps this could help you: http://msdn.microsoft.com/en-us/library/system.windows.uielement.updatelayout.aspx the document says something about `MeasureCore` and `ArrangeCore` – Silvermind Mar 16 '12 at 10:35
  • No, these methods are not public. – ygoe Mar 16 '12 at 12:00
  • Here you find a [related question](http://stackoverflow.com/q/5035174/620360). – LPL Jul 28 '12 at 13:06

1 Answers1

1

Given a FrameworkElement (element) and I wish to allow it to expand fully and then measure it's size I do the following:

element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
element.Arrange(new Rect(new Point(0, 0), element.DesiredSize));
element.UpdateLayout();
Size sizeElementWantsToBe = element.DesiredSize;

Caveat: I found this question because I'm having problems with this method of triggering layout with Telerik RadGridViews with templated columns. But I doubt you will have that problem.

Hope this helps.

Rampnt
  • 11
  • 1