Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Width/Height of a collapsed control in WPF?

Tags:

c#

vb.net

wpf

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.

like image 548
r1cki Avatar asked Apr 18 '11 18:04

r1cki


2 Answers

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.

like image 67
Kent Boogaart Avatar answered Oct 19 '22 08:10

Kent Boogaart


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.

like image 1
Emond Avatar answered Oct 19 '22 09:10

Emond