-
Notifications
You must be signed in to change notification settings - Fork 799
Description
If I've created an element and added it to the view hierarchy of a window, how can I query the size of that element? Measure(Size(width: .infinity, height: .infinity)) works ok for some controls (such as TextBlock) but others such as Button seem to require a render to occur before they start behaving 'sensibly'. I have a hunch that it could be due to resource dictionaries propagating until the controls get rendered, but there seems to be some other weirdness going on too (the hardcoding part in the next paragraph).
If I create a Button containing a TextBlock and call Measure(Size(width: .infinity, height: .infinity)) before the button gets rendered then I just get the size of the TextBlock. The button's padding and border thicknesses properties both seem to be to 0 and only appear to get their true default values when rendered for the first time. But even if I hardcode the padding and border thicknesses to (11, 5, 11, 6) and (1, 1, 1, 1) respectively (taken from Button_themeresources.xaml in this repo), Measure seems to pretend that they're still zero until the button gets rendered.
If this issue is due to resource dictionaries not getting propagated until the element gets rendered, is there a way to force that to happen earlier? For now I've hardcoded in pre-render adjustment factors which apply to certain controls such as sliders and buttons. That approach is pretty janky though, and seems to have fallen apart now that I'm trying to the same for ComboBox since its initial pre-render measurements seem completely unrelated to the size it actually ends up with (e.g. 32x105 pre-render and 71x32 on-screen in one scenario).
For context, I'm writing a WinUI backend for a UI framework I'm working on, so my solution space is a little bit constrained. I need to query sizes of certain controls to feed them back into my UI frameworks custom layout system. I'm also doing this all from Swift, but that shouldn't really affect this issue much. A C# answer is completely fine.