Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to get the size of a control before it's rendered? #10278

Open
stackotter opened this issue Jan 7, 2025 · 0 comments
Open

Question: How to get the size of a control before it's rendered? #10278

stackotter opened this issue Jan 7, 2025 · 0 comments
Labels
needs-triage Issue needs to be triaged by the area owners

Comments

@stackotter
Copy link

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.

@stackotter stackotter changed the title Question: How to get the size of an element before it's rendered? Question: How to get the size of a control before it's rendered? Jan 7, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage Issue needs to be triaged by the area owners
Projects
None yet
Development

No branches or pull requests

1 participant