Sizing a control before rendering

I have a composite control consisting of a rectangle and several dynamically created labels (dynamically, because during development I don't know how many shortcuts should be displayed). Label positions (fields) are evaluated at runtime by combining the entire control size, an additional collection passed through the dependency property, and the heights of the labels themselves.

Unfortunately, I did not decide on the deterministic way of determining the height of the inscription before rendering was done. ActualWidth and ActualHeight are 0 before the labels are displayed, width / height is not set, because I want the labels to determine their contents, DesiredSize returns either 0, the correct size or size that exceeds the real label size (for example, 2 or 3 times), RenderSize returns either the actual size or 0, and it looks like the first label returns a valid size, and the second returns 0.0, for no apparent reason.

I tried calling Measure() on labels with double.PositiveInfinity, which passed only to achieve a situation where DesiredSize was much larger than expected (all labels have the same font and consist only of numbers, so they should all have a more similar size, but first had ~ 16 pixels, the second ~ 36, although after rendering RenderSize was valid for both of them).

Is there a deterministic way to check the desired control size based only on its contents (and not on alignment / borders) before it is displayed on the screen?

+6
source share
1 answer

You can use UpdateLayout to force a measure / layout pass. After calling this method, ActualWidth and ActualHeight will have the correct values.

+9
source

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


All Articles