The best answers to the question “Difference between Visibility.Collapsed and Visibility.Hidden” in the category Dev.
What are differences between
Visibility.Hidden in WPF?
Visibility : Hidden Vs Collapsed
Consider following code which only shows
three Labels and has second
<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>
Now change the second
<Label Width="50" Content="Second" Visibility="Hidden"/>
As simple as that.
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.
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.