Question

I'm working on some layout stuff with XAML/C# and for certain reasons I am unable to use the SizeToContent="Width" option however I would like to simulate it in certain situations. I have a user control sitting inside of an expander and I would like to be able to get the ActualWidth of that user control so that when the expander is expanded I can increase the size of the GridColumn and window accordingly. Because the Expander starts off as IsExpanded="False" the ActualWidth property reads as 0.0. Is there any way I can get the ActualWidth of the user control as if it were fully visible and SizeToContent="Width" were in effect?

I've been trying desperately to figure out how to calculate this width without hardcoding the actual value. DesiredSize.Width and RenderedSize.Width also both return 0.0 when the expander is collapsed

Was it helpful?

Solution

I believe that if you run a Measure & Arrange pass, passing in an infinite Size, you'll be able to get the DesiredSize.

var infiniteSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
control.Measure(infiniteSize);

control.Arrange(new Rect(infiniteSize));

correction:

As mentioned in the comments, just calling Measure() with infinite Size does the trick; do not use Arrange().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top