Finding the actual width of the user control

I am working on some layout materials with XAML / C #, and for certain reasons I cannot use the SizeToContent="Width" parameter, but I would like to simulate it in certain situations. I have a user control sitting inside the expander, and I would like to get the ActualWidth of this user control so that when expanding the expander, I can increase the size of the GridColumn and the window, respectively. Because Expander starts as IsExpanded="False" , the ActualWidth property reads as 0.0. Is there a way to get the ActualWidth of the user control as if it were fully visible and SizeToContent="Width" acted?

I was desperately trying to figure out how to calculate this width without hard coding the actual value. DesiredSize.Width and RenderedSize.Width also return 0.0 when unpacking the expander

+4
source share
1 answer

I believe that if you run Measure & Arrange , which runs in infinite Size , you can get 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 an infinite Size does the trick; do not use Arrange() .

+2
source

Source: https://habr.com/ru/post/1479005/


All Articles