Question: How to get the size of a control before it's rendered? #10292
stackotter
started this conversation in
General
Replies: 1 comment
-
There is a DesiredSize property on Framework element that may be useful, please take a look at the docs for more info: https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.uielement.desiredsize?view=windows-app-sdk-1.6#microsoft-ui-xaml-uielement-desiredsize One thing to note, from the docs, so unsure if this solves your problem.
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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 asTextBlock
) but others such asButton
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 aTextBlock
and callMeasure(Size(width: .infinity, height: .infinity))
before the button gets rendered then I just get the size of theTextBlock
. The button's padding and border thicknesses properties both seem to be to0
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 fromButton_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.
Beta Was this translation helpful? Give feedback.
All reactions