What are the differences between Visibility.Hidden and Visibility.Collapsed?

Asked

Viewed 297 times

2

What are the differences between the visibilities Visibility.Hidden and Visibility.Collapsed of the WPF?

There are practical or theoretical differences between set the Height and the Width of a control to zero and set its visibility to Visibility.Hidden or Visibility.Collapsed? If yes, what would be the differences for each of the two modes?

2 answers

5


Obviously both prevent the element from being displayed.

Visibility.Hidden reserves the space that the element will occupy in the layout, so nothing will be redesigned except in this area. Everything is fixed in place, hiding or not. It just hides, but the element is there.

Visibility.Collapsed does not reserve the space and clean the layout, may require redesign of other elements to adjust positions. It arrow the height and length to zero and the element is no longer there.

If you just change the width and height element will change the layout, equal to Collapsed, but the element remains present, equal to Hidden, even though invisible, maintaining characteristics of something available, active. For example it can gain focus when the controls are changing elements in navigation, probably with TAB. There could be other consequences. Unless you want to exactly make the element exit the screen, taking away its space, but stay there, it is wrong to use this technique.

Documentation.

3

Visibility.Hidden makes the object invisible but reserves its space in the layout.

Visibility.Collapsed makes the object invisible and makes it occupy no space in the layout.

Already set the Widthor Height as zero I do not see much sense since this would only make it more difficult to hide and show the element, because to display again I would need to know the size that this element should have, not counting the cases where do not use the Width and Height to define the size of the elements.

  • Assuming I no longer need to show the element, there would be differences between setting the height and width to zero and using the Collapsed?

  • 2

    @Viníciusbrazil this is complicated to say because it would need to know how the WPF treats the elements, because I don’t know if it would try to render the element even with zero size, I can only imagine in case the element might have focus, even with zero size it is still visible so I imagine that the focus might end up in it yet, which would not happen if I used the Hiddenor Collapsed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.