wpf - Difference between Visibility.Collapsed and Visibility.Hidden

ID : 10020

viewed : 57

Tags : wpfwpf-controlsvisibilitywpf

Top 5 Answer for wpf - Difference between Visibility.Collapsed and Visibility.Hidden

vote vote

94

The difference is that Visibility.Hidden hides the control, but reserves the space it occupies in the layout. So it renders whitespace instead of the control. Visibilty.Collapsed does not render the control and does not reserve the whitespace. The space the control would take is 'collapsed', hence the name.

The exact text from the MSDN:

Collapsed: Do not display the element, and do not reserve space for it in layout.

Hidden: Do not display the element, but reserve space for the element in layout.

Visible: Display the element.

See: http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx

vote vote

85

Visibility : Hidden Vs Collapsed

Consider following code which only shows three Labels and has second Label visibility as Collapsed:

 <StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Center">     <StackPanel.Resources>         <Style TargetType="Label">             <Setter Property="Height" Value="30" />             <Setter Property="Margin" Value="0"/>             <Setter Property="BorderBrush" Value="Black"/>             <Setter Property="BorderThickness" Value="1" />         </Style>     </StackPanel.Resources>     <Label Width="50" Content="First"/>     <Label Width="50" Content="Second" Visibility="Collapsed"/>     <Label Width="50" Content="Third"/> </StackPanel> 

Output Collapsed:

Collapsed

Now change the second Label visibility to Hiddden.

<Label Width="50" Content="Second" Visibility="Hidden"/> 

Output Hidden:

Hidden

As simple as that.

vote vote

74

Even though a bit old thread, for those who still looking for the differences:

Aside from layout (space) taken in Hidden and not taken in Collapsed, there is another difference.

If we have custom controls inside this 'Collapsed' main control, the next time we set it to Visible, it will "load" all custom controls. It will not pre-load when window is started.

As for 'Hidden', it will load all custom controls + main control which we set as hidden when the "window" is started.

vote vote

64

vote vote

52

why is it that even managed languages provide a finally-block despite resources being deallocated automatically by the garbage collector anyway?

Actually, languages based on Garbage collectors need "finally" more. A garbage collector does not destroy your objects in a timely manner, so it can not be relied upon to clean up non-memory related issues correctly.

In terms of dynamically-allocated data, many would argue that you should be using smart-pointers.

However...

RAII moves the responsibility of exception safety from the user of the object to the designer

Sadly this is its own downfall. Old C programming habits die hard. When you're using a library written in C or a very C style, RAII won't have been used. Short of re-writing the entire API front-end, that's just what you have to work with. Then the lack of "finally" really bites.

Top 3 video Explaining wpf - Difference between Visibility.Collapsed and Visibility.Hidden

Related QUESTION?