im wondering if theres an easy way to get the width of a control in WPF at runtime while the control is collapsed.
when i use control.Width ill get the following result: -1.#IND and control.actualWidth will return 0.0 because its collapsed.
i want to resize my window and then display the collapsed control.
thanks
Edit:
Some details
i have a grid with 2 columns in my window, the 1st column holds a tab control, the 2nd column holds an expander control. i want to extend the width of my window when expanding the expander control, so the content in the 1st column will remain its size.
Put the control in question inside a container (like a ContentControl
) and collapse the container rather than the control itself. Then you should be able to simply call Measure
(or use the DesiredSize
property) on the control to determine how much room it wants.
What size do you expect to get?
The size is not just dependent on the control but also on its container. So the actual size can not be determined unless the control is actually rendered.
Instead of using Collapsed you could make it Invisible that way it will be sized by its own logic and the logic of the container.
EDIT
In the comments it became clear that what the reason was for needing the size of the control:
I have a grid with 2 columns in my window, the 1st column holds a tab control, the 2nd column a holds an expander control. i want to extend the width of my window when expanding the expander control, so the content in the 1st column will remain its size.
My answer:
Set the SizeToContent of the window to WidthAndHeight and set the width of both grid columns to auto. That should take care of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With